連接:http://acm.hdu.edu.cn/showproblem.php?pid=5414php
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 int n,flag,m,l1,l2; 7 char c[1100000],ch[1100000]; 8 int main() 9 { 10 scanf("%d",&n); 11 while (n--) 12 { 13 scanf("%s %s",c,ch); 14 l1=strlen(c); 15 l2=strlen(ch); 16 if (l1>l2)//s的長度比T的長度大確定不符合條件 17 { 18 printf("No\n"); 19 continue; 20 } 21 if (c[0]!=ch[0])//第一個字符不相等意味着一開始就要插入,不符合條件 22 { 23 printf("No\n"); 24 continue; 25 } 26 int i,j; 27 for (i=0,j=0;i<l1&&j<l2;j++) 28 { 29 if(ch[j]==c[i]) 30 i++; 31 } 32 if (i<l1)//S須要是T的子串,按順序每一個字符都要在T中找到 33 { 34 printf("No\n"); 35 continue; 36 } 37 i=1,j=1; 38 while (i<l1&&c[i]==c[0]) 39 i++; 40 while (j<l2&&ch[j]==ch[0]) 41 j++; 42 if (i<j)//不能再開頭插入同樣與第一個字符相等的, 43 //好比S爲aat T爲aaat 這種狀況必須在a的後面插入a 44 { 45 printf("No\n"); 46 continue; 47 } 48 printf("Yes\n"); 49 } 50 return 0; 51 }