码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5414 CRB and String

时间:2015-08-28 19:18:49      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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

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 }

 

 

HDU 5414 CRB and String

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4767025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!