CRB and String(HDUOJ--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".

题意:输入两串字符串s和t,在s中选择一个字母在其后边插入一个与其不同的字母,可以插入无限次,问通过插入操作是否能使s变为t。

思路:如果直接暴力遍历铁定会超时啊(233~。经过大神教导,该题是有规律的,只要该两个字符串符合两个条件就可以通过插入操作使s变为t。条件一:字符串s中字母出现的次数要小于等于字符串t中该字母出现的次数(毕竟是往字符串s中插入字母,如果s中的某字母比t中的多那么字符串s通过插入操作怎么也不可能变成t);条件二:字符串s的首字母与字符串t的首字母要相等,且如果两个字符串的开头是连续的相同字母,则s开头连续的相同字母的个数要大于等于t开头连续的相同字母个数(如果连续的相同字母出现在字符串中间,则可以通过在该字母之前插入其他字母将该连续的相同字母分开)。只要满足以上两个条件则可达到题目要求输出“Yes”,否则输出“No”.

Sample Input

4

a

b

cat

cats

do

do

apple

aapple

Sample Output

No

Yes

Yes

No

<span style="font-size:18px;">#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
#define esp 1e-9
using namespace std;
int s1[200],t1[200];
char s[110000],t[110000];
int main()
{
    //freopen("lalala.text","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%s",s);
        scanf("%s",t); 
        if(s[0]!=t[0])                     //如果两个字符串的首字母都不同则肯定不会达到题目要求
        {
          printf("No\n");
          continue;
        }
        memset(s1,0,sizeof(s1));
        memset(t1,0,sizeof(t1));
        int slen=strlen(s);
        int tlen=strlen(t);
        int ss=0,tt=0;
        char ch=s[0];
        int flag=0;
        for(int i=0; i<slen; i++)
        {
            if(s[i]==ch&&!flag)
                ss++;                    //累加字符串开头连续相同字母的个数
            else
                flag=1;
            s1[s[i]]++;
        }
        ch=t[0];
        flag=0;
        for(int i=0; i<tlen; i++)
        {
            if(t[i]==ch&&!flag)
                tt++;
            else
                flag=1;
            t1[t[i]]++;
        }
        flag=1;
        if(ss<tt)
        flag=0;
        else
        {
            for(int i=95; i<125; i++)
            {
                if(s1[i]>t1[i])
                {
                    flag=0;
                    break;
                }
            }
        }
        if(flag)
        printf("Yes\n");
        else
        printf("No\n");
    }
    return 0;
}</span><span style="font-weight: bold; font-size: 14px;">
</span>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值