hdu 5414 CRB and String 2015多校联合训练赛#10 贪心

CRB and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 246    Accepted Submission(s): 90


Problem Description
CRB has two strings  s  and  t .
In each step, CRB can select arbitrary character  c  of  s  and insert any character  d  ( d  c ) just after it.
CRB wants to convert  s  to  t . But is it possible?
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case there are two strings  s  and  t , one per line.
1 ≤  T  ≤  105
1 ≤  |s|  ≤  |t|  ≤  105
All strings consist only of lowercase English letters.
The size of each input file will be less than 5MB.
 

Output
For each test case, output "Yes" if CRB can convert s to t, otherwise output "No".
 

Sample Input
   
   
   
   
4 a b cat cats do do apple aapple
 

Sample Output
   
   
   
   
No Yes Yes No
 

Author
KUT(DPRK)
 

Source
2015 Multi-University Training Contest 10

首先保证前缀要一样,然后逆序贪心做,能匹配就匹配,不然就添加。

最后查看是否全部匹配成功了,如果还有t中还有字符,判段最右边的是否跟开头一样就行



#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define maxn 100007

using namespace std;
char s[maxn],t[maxn];
int lef1[maxn],lef2[maxn];

int main(){
    int tt;
    scanf("%d",&tt);
    while(tt--){
        scanf("%s",s);
        scanf("%s",t);
        int l1 = strlen(s), l2 = strlen(t);
        int flag = 1;
        if(s[0] != t[0] || l1 > l2)
            flag = 0;
        int i = l1-1,j=l2-1,c1=1,c2=1;
        while(s[c1] == t[c2] && s[c1] == s[0]&& c1 < l1 && flag)c1++,c2++;
        for(; j >= c1; j--){
            if(i == c2-1) break;
            if(t[j] ==  s[i] && i > 0)i--;

        }
        if(i > c2-1) flag = 0;
        if(s[c1-1] == t[c1])flag = 0;
        if(flag)puts("Yes");
        else puts("No");
    }
    return 0;
}
/*
40
a
b

cat
cats

do
do

apple
aapple

aaa
aaaaa

aaaattaa
aaaattaaaa

apple
allpple

aaa
abaaaaa

aaa
aaabaaa
aaa
aaaabaaa

*/


你可能感兴趣的:(String,HDU,贪心,and,CRB,2015多校联合训练赛#10,5414)