1. 程式人生 > >4300(擴充套件kmp 求字尾的最大字首)

4300(擴充套件kmp 求字尾的最大字首)

Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion table.  Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.  But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.  Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem. 

Input

The first line contains only one integer T, which is the number of test cases.  Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete. 

Hint

Range of test data:  T<= 100 ;  n<= 100000; 

Output

For each test case, output one line contains the shorest possible complete text.

Sample Input

2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde

Sample Output

abcdabcd
qwertabcde

題意:

首先t組輸入

第一行給你26個字元,即字串S1, 表示字元的加密方式,即a~z中的第i個字元加密後變為為S1[i]

第二行一個擷取的字串,這個字串的前部分是情報的密文,後面是情報明文的一部分(因為傳送情報的人發現自己被偵察了就不發了),

讓你求這個最短的完整資訊

重點:

密文的長度一定滿足temp>=(len+1)/2  這樣才符合題意 ,

將該資訊解密後的字首解密後的字尾最大匹配  表示最長的可能明文,注意這個最長的可能明文長度不能超過len-temp

問題就改成了兩個字串求字首字尾最大匹配(擴充套件kmp)

#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
const int MOD=1e5+7;
int code[100];//將密文轉化為明文
char convert(char c)
{
    return code[c-'a']+'a';
}
char infostr[maxn];
char codestr[maxn];
int net[maxn];//存放info的next陣列
int maxpp[maxn];//最長字尾匹配字首陣列
void get_nextval(char *s,int* net)
{
    int ls=strlen(s);
    int j=0,k=-1;
    net[0]=-1;
    while(j<ls)
    {
        if(k==-1||s[j]==s[k])
            net[++j]=net[++k];
        else
            k=net[k];
    }
}
int exkmp(char *s,char *p,int *net)
{
    int ls=strlen(s);
    int temp=(ls+1)/2;//代表最大結尾的匹配不能超過
    get_nextval(p,net);
    int i=temp,k=0;
    /*
    重點:  文字串的第temp+1個字元與模式串的第1個字元開始匹配 因為前temp字元肯定是密文,不能讓
    字首與密文匹配到 ,這樣匹配得到的字首才肯定全都是密文的
    擴充套件kmp:  計算此串的最大匹配字首時,利用上一個串的最大匹配字首
    */
    while(i<ls)
    {
        if(k==-1||s[i]==p[k])//第i+1個字元與第k+1個字元是否相等
        {
             ++i;
             ++k;//代表第i+1個字元的最大匹配為k+1個
             maxpp[i]=k;
        }
        else
            k=net[k];
    }
    return maxpp[ls];
}
int main()
{
    int t,ans;
    char bm[30];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",bm);
        for(int i=0;i<26;++i)
            code[bm[i]-'a']=i;
        scanf("%s",infostr);
        int len=strlen(infostr);
        for(int i=0;i<len;++i)
            codestr[i]=convert(infostr[i]);
        ans=exkmp(infostr,codestr,net);
        printf("%s",infostr);
        for(int i=ans;i<len-ans;++i)
            printf("%c",convert(infostr[i]));
        printf("\n");
    }
}