2015 暑假多校训练---CRB and String(简单但有很多需要注意地方)

题目链接
http://acm.hust.edu.cn/vjudge/contest/131172#problem/B

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串,操作规则是对s串中的任意一个字符x,可以在其后面添加除x外的任意字符;

思路:对t串从头开始遍历,令k=0,表示当前s串遍历到的位置,若t[i]==s[k], i++;k++; 若不想等则i++,k不变(就是在s[k-1]后添一个字符,注意判断是否添加成功,因为可能前面全是同一个字符且t[i-1]==t[i],添加不成功时,即不能由s->t) 最后注意判断一下k==|s|
还有要注意特判s[0] t[0] ,因为s串前不可能添加字符;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define eps 1e-8
#define maxn 105
#define inf 0x3f3f3f3f3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
char s[100005],t[100005];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%s%s",s,t);
        int len1=strlen(s);
        int len2=strlen(t);
        if(s[0]!=t[0])  {puts("No");continue;}
        int k=1;
        int flag=1;
        int f=0;
        for(int i=1;i<len2;i++)
        {
            if(s[k]==t[i]) {
                if(t[i]!=t[0])
                f=1;
                k++; continue;
            }
            else if(t[i]==t[i-1]&&f==0) {flag=0; break;}
            else f=1;
        }
        if(flag&&k==len1) puts("Yes");
        else     puts("No");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值