HDU5414 CRB and String
? 解题记录 ? ? HDU ?    2018-01-16 15:21:11    435    0    0
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
 


Author
KUT(DPRK)
 


Source
 


Recommend
wange2014   |   We have carefully selected several similar problems for you:  6253 6252 6251 6250 6249 

不难发现这是一道结论题。首先s串必须是t串的子序列,并且开头相等。我们发现对于不是开头连续的缺失字符可以直接在前一个字符前添加,比如:要把aaac转换成abbdbbaac只需要一直在第一个a后面添加缺失的字符就好了。然后考虑如果两个串前面有一串相同字符组成的连续的前缀。如果第二个串的前缀比第一个串长,那么由于c != d我们可以得知他们不能相互转换,否则如果满足s是t的子序列,它们仍然能转换。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5;
char a[maxn], b[maxn], c;
int t, lena, lenb, pos, flag;
int main() {
	scanf("%d", &t);
	while(t--) {
		scanf("%s%s", a, b);
		lena = strlen(a), lenb = strlen(b);
		if(a[0] != b[0]) {printf("No\n"); continue;}
		for(pos = 1; a[pos - 1] == a[pos] && a[pos] == b[pos] && pos < lena && pos < lenb; ++pos);
		c = a[pos - 1];
		if(b[pos] == c) {printf("No\n"); continue;}
		pos = 0, flag = 0;
		for(register int i = 0; i < lena; ++i) {
			while(b[pos] != a[i]) {
				++pos;
				if(pos > lenb) 
					{flag = 1; break;}
			}
		}
		if(flag) printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}


上一篇: BZOJ3771: Triple

下一篇: HDU6138 Fleet of the Eternal Throne

435 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航