HDU 5414 CRB and String

連接:http://acm.hdu.edu.cn/showproblem.php?pid=5414php

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
 
一開始題意理解錯了,搞了老半天沒搞出來,看了別人的博客才恍然大悟。
 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 }
相關文章
相關標籤/搜索