GZS and String

19 篇文章 0 订阅
6 篇文章 0 订阅

GZS and String

https://qduoj.com/problem/6?tdsourcetag=s_pctim_aiomsg

Description

 

GZS has two strings s and t.

In each step, GZS can select arbitrary character c of s and insert any character d (d ≠ c) just after it.

GZS 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 ≤ 10^5 1 ≤ |s| ≤ |t| ≤ 10^5 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 GZS can convert s to t, otherwise output "No".

Sample Input 1 

4
a
b
cat
cats
do
do
apple
aapple

Sample Output 1

No
Yes
Yes
No

题意:给你两个字符串s和t,你可以在字符串s中任意选一个字符c,在该字符c后插入一个字符d(d!=c),问经过多次此操作,能否将字符串s转化成字符串t

思路:

1.如果t串前j个字符相同,那么s串前j个字符也必须相同(因为插入的字符d!=c) 

例:abc

        aabc

        No

2.s串必须是t串的子序列

满足这两个条件的就Yes

aacc

aacccc

Yes

(虽然c后不能插入c但是可以在a后面插入)

#include<cstdio>
#include<cstring>
using namespace std;
char s[100005],t[100005];
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%s%s",s,t);
		int ls=strlen(s),lt=strlen(t);
		int i,j;
		bool flag=true;
		for(j=1; j<lt; j++) {     //判断t串前面有多少个相同的字母 
			if(t[j]!=t[0]) break;
		}
		for(i=0; i<j; i++) {     //判断s串前j个字母是否与t串一样 
			if(s[i]!=t[0]) {     //如果不一样肯定No(无法插入) 
				flag=false;       
				break;
			}
		}
		if(flag) {               //再判断s串是不是t串的子序列 
			for(i=0,j=0; j<lt;) {  
				if(s[i]==t[j]) {
					i++;
					j++;
				} else
					j++;
			}
			if(i<ls) {           //如果不是就No 
				flag=false;
			}
		}
		if(flag) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值