HDU 4300(擴展KMP)

Problem Description
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

題目大意:給出一個密碼錶,然後給出一個密文和原文,原文可能不是完整的,問由完整的密文和原文組成的字符串最短是怎樣的。

解題思路:將密碼解析出來,然後使得後面的原文與密文相等即可。所以使用擴展KMP算法跑一遍最長相等前綴,然後選擇最短滿足的密文長度就可以啦。

代碼:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int inf=1ll*1<<60;
const int maxn=2e5+10;
const int maxm=3e6+10;
char arr[maxn],str[maxn],mp[300],op[300];
int to[maxn],extend[maxn];
void getto()
{
    to[0]=strlen(arr);
    int a=0,p=0;
    for(int i=1;arr[i]!='\0';i++){
        if(i>=p || i+to[i-a]>=p){
            if(i>=p)p=i;
            while(arr[i]!='\0' && arr[p]==arr[p-i])p++;
            to[i]=p-i;
            a=i;
        }
        else to[i]=to[i-a];
    }
}
void getext()
{
    int a=0,p=0;
    getto();
    for(int i=0;str[i]!='\0';i++){
        if(i>=p || i+to[i-a]>=p){
            if(i>=p)p=i;
            while(str[p]!='\0' && str[p]==arr[p-i])p++;
            extend[i]=p-i;a=i;
        }
        else extend[i]=to[i-a];
    }
}
signed main()
{
    int t;
    scanf("%lld",&t);
    while(t--){
        memset(to,0,sizeof to);
        memset(extend,0,sizeof extend);
        scanf("%s%s",op,str);
        for(int i=0;i<26;i++){
            mp[op[i]-'a']=i+'a';
        }
        memset(arr,0,sizeof arr);
        for(int i=0;str[i]!='\0';i++){
            arr[i]=mp[str[i]-'a'];
        }
        //cout<<arr<<endl;
        getext();
        int ans,len=strlen(str);
        ans=len;
        for(int i=0;str[i]!='\0';i++){
            if(i>=extend[i] && i+extend[i]>=len){
                ans=i;
                break;
            }
        }
        //cout<<ans<<endl;
        for(int i=0;i<ans;i++){
            printf("%c",str[i]);
        }
        for(int i=0;i<ans;i++){
            printf("%c",mp[str[i]-'a']);
        }
        puts("");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章