From S To T

这是一道水题,但是因为某些逻辑上的细节没注意到或者把简单问题想复杂了导致改了很长时间的BUG,是因为标记的状态量用得太多,没有用好(逻辑没有理清,没想全),主要是漏掉了一种情况,结果一直没发现,所以以后敲代码之前一定要把情况考虑周全,不要心急,等思路理清了再动手敲,一定要讲究逻辑严谨!

后来静下心来重新做这个题,一次就交对了,发现判断每个字符在前两个串中的相对位置是否相等时,只需要加两个指针,只要两字符相等,则两指针同时后移,否则只有第二个指针后移,这样最后只要判断一下两个串是否遍历完就行了。在判断第三个串时也换了种思路,有相等的就把那个字符换成别的取不到的值(相当于删掉)就行了

综上所述,在做题时一定不要着急!静下心来,考虑周全,逻辑严谨,如果在比赛中遇到这种实在改不了的BUG,还不如直接考虑重新做这个题或者跳过(回过头来再做)

。。。。下面我们来看一下这个题。。。。

 

问题 I: From S To T

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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.

 

输入

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.
 

 

样例输入

复制样例数据

4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

样例输出

YES
YES
NO
NO

 

提示

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.

完整代码1:一开始做的时候是因为某些逻辑上的细节没注意到或者把简单问题想复杂了导致改了很长时间的BUG,是因为标记的状态量用得太多太乱没有用好(逻辑没有理清,没想全),主要是漏掉了一种情况,结果一直没发现,后来第二次重新做后突然发现一开始代码的错误,才回过头来改对,所以以后敲代码之前一定要把情况考虑周全,不要心急,等思路理清了再动手敲,一定要讲究逻辑严谨!

完整代码2:后来静下心来重新做这个题,一次就交对了,发现判断每个字符在前两个串中的相对位置是否相等时,只需要加两个指针,只要两字符相等,则两指针同时后移,否则只有第二个指针后移,这样最后只要判断一下两个串是否遍历完就行了。在判断第三个串时也换了种思路,有相等的就把那个字符换成别的取不到的值(相当于删掉)就行了

先后两次最终交对的代码:

完整代码1:

#include 
using namespace std;
char s[110],t[110],p[110],p2[110];
int vis[110];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;int cnt=0;
    cin>>n;
    while(n--)
    {
        cin>>s>>t>>p;
        for(int i=0;i<110;i++)
        {
            vis[i]=0;
        }
        int lens=strlen(s),lent=strlen(t),lenp=strlen(p);
        int flag=0,lflag=0,k=0,q=0,g=0,m=0,l=0;
        if(lens>lent)
        {
            flag=0;
        }
        else{
            for(int i=0;i

 

 完整代码2:

#include 
#define int long long
using namespace std;
int n;
char s[110],t[110],p[110];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    while(n--)
    {
        cin>>s>>t>>p;
        int flag=-1,lens=strlen(s),lent=strlen(t),lenp=strlen(p),g=0,l=0;//g,l就是那两个指针变量
        while(l=lent)//注意这里要在再一下断l

 

你可能感兴趣的:(From S To T)