Educational Codeforces Round 68 (Rated for Div. 2)-C-From S To T

8585 단어
You are given three strings ss, tt and pp consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose any character from pp, erase it from pp and insert it into string ss (you may insert this character anywhere you want: in the beginning of ss, in the end or between any two consecutive characters).
For example, if pp is aba, and ss is de, then the following outcomes are possible (the character we erase from pp and insert into ss 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 ss becomes equal to tt. Please determine whether it is possible.
    Note that you have to answer qq independent queries.
    Input
    The first line contains one integer qq (1≤q≤1001≤q≤100) — the number of queries. Each query is represented by three consecutive lines.
    The first line of each query contains the string ss (1≤|s|≤1001≤|s|≤100) consisting of lowercase Latin letters.
    The second line of each query contains the string tt (1≤|t|≤1001≤|t|≤100) consisting of lowercase Latin letters.
    The third line of each query contains the string pp (1≤|p|≤1001≤|p|≤100) consisting of lowercase Latin letters.
    Output
    For each query print YES if it is possible to make ss equal to tt, 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
    input
    Copy
    4
    ab
    acxb
    cax
    a
    aaaa
    aaabbcc
    a
    aaaa
    aabbcc
    ab
    baaa
    aaaaa
    

    output
    Copy
    YES
    YES
    NO
    NO
    

    Note
    In the first test case there is the following sequence of operation:
  • s=s= ab, t=t= acxb, p=p= cax;
  • s=s= acb, t=t= acxb, p=p= ax;
  • s=s= acxb, t=t= acxb, p=p= a.

  • In the second test case there is the following sequence of operation:
  • s=s= a, t=t= aaaa, p=p= aaabbcc;
  • s=s= aa, t=t= aaaa, p=p= aabbcc;
  • s=s= aaa, t=t= aaaa, p=p= abbcc;
  • s=s= aaaa, t=t= aaaa, p=p= bbcc.

  • 코드:
    #include
    #include
    #include
    #include
    #include
    #include
    #include<set>
    #include
    #include
    #include
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    string s,t,p;
    int vis[maxn];
     
    int dp[105][105];
    string str;
    int main()
    {
        
       int T;
       cin>>T;
       while(T--)
       {
           cin>>s>>t>>p;
           str=s+p;
           memset(vis,0,sizeof(vis));
           int len1=t.length();
           int len2=str.length();
           int len3=s.length();
           int ans=0;
           memset(dp,0,sizeof(dp));
           for(int i=1;i<=len1;i++)
           {
               for(int j=1;j<=len3;j++)
               {
                   if(t[i-1]==s[j-1])
                   {
                       dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
                }
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        int flag=0;
        if(dp[len1][len3]==len3)
        {
            flag=1;
        }
        if(flag==0)
        {
            puts("NO");
            continue;
        } 
        else
        {
            for(int i=0;i)
            {
                for(int j=0;j)
                {
                    if(str[j]==t[i]&&vis[j]==0)
                    {
                        vis[j]=1;
                        ans++;
                        break;
                    }
                }
            }
            if(ans==len1)
        {
            puts("YES"); 
        }
        else
        {
            puts("NO");
        }
        }
        
       } 
       return 0;
    }

     
    전재 대상:https://www.cnblogs.com/Staceyacm/p/11190374.html

    좋은 웹페이지 즐겨찾기