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 }
相关文章
相关标签/搜索