hdu5414 CRB and String

本文介绍了一个字符串转换问题,即通过在给定字符串的任意字符后添加不同字符的方式,判断是否能将一个字符串转换为另一个字符串。文章提供了一种解题思路及实现代码。

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

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

题意:给你两个字符串s1,s2,你可以在s1中选择任意一个字符,并且在后面添加任意与之不同的字符在后面,如果添加若干或者不添加能使s1变成s2,就输出Yes,否则输出No.

思路:只要满足两个条件就输出Yes:1.两个字符串首字母必须相等且第一个字符串中首字母连续的个数必须小于等于第二个字符。2.对于任意一个字符c,它在第一个字符串中的个数要小于等于第二个字符串的。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100050
char s1[maxn],s2[maxn];
int num1[27],num2[27];
int main()
{
    int n,m,i,j,T,len1,len2,flag1,sum1,sum2,flag2,flag;
    char c1,c2;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",s1,s2);
        if(strcmp(s1,s2)==0){
            printf("Yes\n");continue;
        }
        len1=strlen(s1);
        len2=strlen(s2);
        c1=s1[0];c2=s2[0];
        if(c1!=c2){
            printf("No\n");continue;
        }
        memset(num1,0,sizeof(num1));
        memset(num2,0,sizeof(num2));
        sum1=sum2=0;flag1=flag2=1;
        for(i=1;i<len1;i++){
            if(flag1 && s1[i]==s1[0])sum1++;
            else flag1=0;
            num1[s1[i]-'a'+1]++;
        }
        for(i=1;i<len2;i++){
            if(flag2 && s2[i]==s2[0])sum2++;
            else flag2=0;
            num2[s2[i]-'a'+1]++;
        }
        if(sum1<sum2){
            printf("No\n");continue;
        }
        flag=1;
        for(i=1;i<=26;i++){
            if(num1[i]>num2[i]){
                flag=0;break;
            }
        }
        if(flag)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值