HDU 5414 CRB and String 思维好题

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

题意:本题要求判断字符串s能否通过添加若干个字符得到字符串t。

解法:思维好题,像窝这种弱鸡真心想不到啊,首先,可以知道,s必须是t的一个子串(注意:不是连续子串)。第二,由于插入的新字符和它前面的字符c不同,因此如果t中有cnt个连续的c,那么在s中也必须有cnt个连续的c。因此,只要能够满足这2个条件,就一定可以成功实现转化。然后手推了很多样例发现确实是这样妙啊233。

#include <bits/stdc++.h>
using namespace std;
const int maxn  = 100010;
char s1[maxn], s2[maxn];
bool check(char *s1, char *s2)
{
    int n = strlen(s1);
    int m = strlen(s2);
    int i,j;
    for(j=1; j<m; j++) if(s2[j]!=s2[0]) break;
    for(i=0; i<j; i++) if(s1[i]!=s2[i]) return false;
    while(i<n)
    {
        for(;j<m;j++){
            if(s2[j]==s1[i]){
                break;
            }
        }
        if(j == m) return false;
        i++,j++;
    }
    return true;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", s1);
        scanf("%s", s2);
        if(check(s1, s2)){
            puts("Yes");
        }
        else{
            puts("No");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值