多校第十場1009 CRB and String題解

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5414

題意:給你兩個字符串s和t,你可以在字符串s中任意選一個字符c,在該字符c後插入一個字符d(d!=c),問經過多次此操作,能否將字符串s轉化成字符串t

思路:初讀題理解錯題意,以爲只能在原串的每個字符後面添加一個不同於這個字符的一個字母,事實上可以重複操作,比如,ap,轉換成axxp,可以先在a後面添加一個x,就變成了axp,再次操作,在a後面添加一個x,就可以變成axxp。

換句話說:就是保證若第二個字符串t前k個字符都相同,那麼第一個字符串s的前k個字符也必須相同(1),剩下的,即使出現插入的字母和前一個字符相同,我們也可以理解成前一個字符是插在前面那個字符的後一個的字符(2),好吧,說暈了,舉個栗子:apple,轉換成appple,如果第二個p是插入到p後面的字符,按照題目要求是不可以的,但是同時我們可以把第一個p當成插在a後面的字符,這樣就符合條件了,這就是之前(2)的解釋;再舉個栗子:apple,轉換成aapple,後一個字符串前兩個字符都是a,而第一個字符串只有一個a字符,這樣第二個字符串中的第二個字符a就必須是a後面插入的,這樣明顯就不符合條件,ok,(1)解釋完了。。

具體操作步驟,就是先保證若字符串t前k個字符都相同,那麼字符串s的前k個字符也必須相同,然後在剩下的字符串中按順序找到第一個字符串的每個字母就行

代碼:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 100005;
char str1[maxn],str2[maxn];
int l1,l2;
int check()
{
      int i,j;
        for(i=0;i<l2;i++)
        {
            if(str2[i]!=str2[0])
                break;
        }
        for(j=0;j<i;j++)
        {
            if(str1[j]!=str2[j])
                return 0;
        }
        while(j<l1)
        {
            for(;i<l2;i++)
            {
                if(str1[j]==str2[i])
                    break;
            }
            if(i==l2)
                return 0;
            i++;
            j++;
        }
        return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",str1,str2);
         l1=strlen(str1);
         l2=strlen(str2);
        if(l2<l1)
        {
            printf("No\n");
            continue;
        }
        if(check())
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章