您的位置:首页 > 其它

CRB and String(HDUOJ--5414

2015-08-20 21:02 288 查看
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?

[b]Input

[/b]

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.

[b]Output

[/b]

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”.

[b]Sample
Input


[/b]

4

a

b

cat

cats

do

do

apple

aapple

[b]Sample
Output


[/b]

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: