hdu 5414 CRB and String 2015多校联合训练赛#10 贪心

CRB and String

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


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

首先保证前缀要一样,然后逆序贪心做,能匹配就匹配,不然就添加。

最后查看是否全部匹配成功了,如果还有t中还有字符,判段最右边的是否跟开头一样就行



#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define maxn 100007

using namespace std;
char s[maxn],t[maxn];
int lef1[maxn],lef2[maxn];

int main(){
    int tt;
    scanf("%d",&tt);
    while(tt--){
        scanf("%s",s);
        scanf("%s",t);
        int l1 = strlen(s), l2 = strlen(t);
        int flag = 1;
        if(s[0] != t[0] || l1 > l2)
            flag = 0;
        int i = l1-1,j=l2-1,c1=1,c2=1;
        while(s[c1] == t[c2] && s[c1] == s[0]&& c1 < l1 && flag)c1++,c2++;
        for(; j >= c1; j--){
            if(i == c2-1) break;
            if(t[j] ==  s[i] && i > 0)i--;

        }
        if(i > c2-1) flag = 0;
        if(s[c1-1] == t[c1])flag = 0;
        if(flag)puts("Yes");
        else puts("No");
    }
    return 0;
}
/*
40
a
b

cat
cats

do
do

apple
aapple

aaa
aaaaa

aaaattaa
aaaattaaaa

apple
allpple

aaa
abaaaaa

aaa
aaabaaa
aaa
aaaabaaa

*/


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值