HDU 5414 CRB and String

本文探讨了CRB如何通过插入操作将一个字符串s转换为另一个字符串t的问题,重点在于理解字符串之间的转换条件,以及如何判断转换是否可能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: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 }

 

 

转载于:https://www.cnblogs.com/arno-my-boke/p/4767025.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值