From S To T
Hazrat Ali
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.
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.
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).
4 ab acxb cax a aaaa aaabbcc a aaaa aabbcc ab baaa aaaaa
YES YES NO NO
Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int q;
cin >> q;
while (q--)
{
string s, t, p;
cin >> s >> t >> p;
int i = 0;
while (i < t.size() and s.size() < t.size())
{
if (s[i] != t[i])
{
auto pos = p.find(t[i]);
if (pos == string::npos)
{
break;
}
s.insert(i, 1, p[pos]);
p.erase(pos, 1);
}
i++;
}
if (s == t)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}