C. From S To T
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three strings s, t and p consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose any character from p, erase it from p and insert it into string s (you may insert this character anywhere you want: in the beginning of s, in the end or between any two consecutive characters).
For example, if p is aba, and s is de, then the following outcomes are possible (the character we erase from p and insert into s is highlighted):
aba → ba, de → ade;
aba → ba, de → dae;
aba → ba, de → dea;
aba → aa, de → bde;
aba → aa, de → dbe;
aba → aa, de → deb;
aba → ab, de → ade;
aba → ab, de → dae;
aba → ab, de → dea;
Your goal is to perform several (maybe zero) operations so that s becomes equal to t. Please determine whether it is possible.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1≤q≤100) — the number of queries. Each query is represented by three consecutive lines.
The first line of each query contains the string s (1≤|s|≤100) consisting of lowercase Latin letters.
The second line of each query contains the string t (1≤|t|≤100) consisting of lowercase Latin letters.
The third line of each query contains the string p (1≤|p|≤100) consisting of lowercase Latin letters.
Output
For each query print YES if it is possible to make s equal to t, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
inputCopy
4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa
outputCopy
YES
YES
NO
NO
Note
In the first test case there is the following sequence of operation:
s= ab, t= acxb, p= cax;
s= acb, t= acxb, p= ax;
s= acxb, t= acxb, p= a.
In the second test case there is the following sequence of operation:
s= a, t= aaaa, p= aaabbcc;
s= aa, t= aaaa, p= aabbcc;
s= aaa, t= aaaa, p= abbcc;
s= aaaa, t= aaaa, p= bbcc.
题意:
给你3个字符串s,t, p, 询问是否可以从p中取出一些字符插入到字符串s的任意位置,使其等于t ?
思路:
因为s字符串中字符的顺序是没法改变的,所以我们要检测一下s中的字符是否是t中都有的,而且顺序是否一致、
如果一致再根据字符的个数判断就可以了。具体见code
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d
",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[1005];
char t[1005];
char p[1005];
int q;
int need[300];
int cnt[300];
int main()
{
// freopen("D:\common_text\code_stream\in.txt","r",stdin);
//freopen("D:\common_textcode_stream\out.txt","w",stdout);
gg(q);
while (q--)
{
MS0(cnt);
MS0(need);
scanf("%s", s + 1);
scanf("%s", t + 1);
scanf("%s", p + 1);
int slen = strlen(s + 1);
int tlen = strlen(t + 1);
int plen = strlen(p + 1);
if (slen + plen < tlen)
{
printf("NO
");
} else
{
int num = 0;
int id = 1;
repd(i, 1, tlen)
{
if (t[i] == s[id])
{
id++;
num++;
}
}
if (num != slen)
{
printf("NO
");
} else
{
repd(i, 1, tlen)
{
need[t[i]]++;
}
repd(i, 1, slen)
{
cnt[s[i]]++;
}
repd(i, 1, plen)
{
cnt[p[i]]++;
}
int isok = 1;
for (char i = 'a'; i <= 'z'; ++i)
{
if (need[i] > cnt[i])
{
isok = 0;
break;
}
}
if (isok)
{
printf("YES
");
} else {
printf("NO
");
}
}
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '
');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}