问题链接(https://codeforces.com/problemset/problem/1194/C)
问题描述
有三个字符串s,t,p,可以把p中任意字符插入到s的任意位置,但字符不能重复使用,问是否通过这样的操作(也可以不做)使得s变成t。
问题分析
先要求s、p的每种字符的总个数要不少于t的对应字符的个数,然后还得要求s是t的子序列,否则永远无法匹配上。如果s不是t的子序列,那么s总会比t“突出”一些字符,所以就不可能匹配上;如果s是t的子序列后,只需要让p的字符填充上去即可。
代码如下
#include<bits/stdc++.h>
using namespace std;
const int maxn=26;
string s,t,p;
bool solve(){
int i,j,lens,lent,lenp,csp[maxn]={0},ct[maxn]={0};
lens=s.size();
lent=t.size();
lenp=p.size();
for(i=0;i<lens;i++) csp[s[i]-'a']++;
for(i=0;i<lenp;i++) csp[p[i]-'a']++;
for(i=0;i<lent;i++) ct[t[i]-'a']++;
for(i=0;i<maxn;i++) if(csp[i]<ct[i]) return false;
for(i=0,j=0;i<lent;i++){//判断s是否为t的子序列
if(s[j]==t[i]){
j++;
if(j==lens) return true;
}
}
return false;
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int q;
cin>>q;
while(q--){
cin>>s>>t>>p;
if(solve()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}