HDU 5414 CRB and String

CRB and String

                                                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                             Total Submission(s): 257    Accepted Submission(s): 93


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串每个字符s[i]的后面插入任意一个字符d (要求这时的d 不能与s[i]相同)
比如 由"aa" 变为"asaa" 这个过程为 aa -> asa(在'a'的后面插入's') -> asaa(s后面再插入a)
其中一点比较特殊的是 "aa" 可以变为"aasaa" ,过程如下
"aa" -> "aas" -> "aasa" -> "aasaa"
这样一看我们最后一个'a'前面也是个'a',其实这个'a'是在's'后插入的 ,即一个字符s[i]后可以重复多次插入与s[i]不同的 相同的字符 ,说的有点绕 ,其实就是在这个's'后可以重复的插入同一个字符'a'。。
还有一点是 s[0] 前面是不能插入字符的 aa 是不能变成 taa的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define inf 0x3f3f3f3f
#define N 100001
using namespace std;
char s[N],t[N];
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s%s", s, t);
        int lens = strlen(s);
        int lent = strlen(t);
        int k = 0;
        for(int i = 0; i < lent; i++)  // 判断串s中是否含有 t串中不存在的字符
        {
            if(t[i] == s[k])           //s串中字符不能删除 ,如果有直接No
                k++;
            if(k == lens) break;
        }
        if(k < lens || s[0] != t[0])   //首字符不同输出No s串首不能插入 
        {
            printf("No\n");
            continue;
        }
        int ss = 0;
        int tt = 0;
        while(ss < lens)
        {
            if(s[ss] == s[0] && t[tt] == t[0])   //找出s串 和 t 串的公共相同前缀 
            {
                ss++;                            // 如 s : aa     t : aaataa     
                tt++;                            // 公共前缀为 aa
            }                                   //此时若 t串的下一个字符还与前缀字符相同
            else break;                         //即说明s不可能变为t
        }                                       // 因为 aa 无法 变为 aaa
        if(t[tt] == s[0])
         printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值