HDU - 4300——Clairewd’s message

博客已搬遷至:這裏

題目鏈接:https://vjudge.net/problem/HDU-4300

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.
HintRange 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

題意:第一行給出一個字母轉換表a,表示第i位對應的字母轉換成a[i]上的字母,再給出一個字符串,這個字符串前一半是暗文,後一般是明文,明文可能不完整,讓你輸出完整的暗文+明文,要求長度最小

思路:擴展KMP中next數組應用,關於擴展KMP算法可以參考https://segmentfault.com/a/1190000008663857這篇文章
1、關於擴展KMP算法中的extend數組,extend[i]代表的是從文本串S的第i位開始與模式串T的最長匹配,因此我們只要判定出i + extend[i] >= n && i >= extend[i]就找到了明文與暗文的分界位置

代碼:

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int n , m;
int lena , lenb;
char a[N] , b[N];
char S[N] , T[N];
map<char , char>mp;
int Next[N] , extend[N];

void getNext()
{
    int a = 0 , p = 0;
    Next[0] = m;
    
    for(int i = 1 ; i < m ; ++ i)
    {
        if(i >= p || i + Next[i] >= p)
        {
            if(i >= p) p = i;
            while(p < m && T[p] == T[p - i])
                ++ p;
            Next[i] = p - i;
            a = i;
        }
        else Next[i] = Next[i - a];
    }
}
void getExtend()
{
    int a = 0 , p = 0;
    getNext();
    for(int i = 0 ; i < n ; ++ i)
    {
        if(i >= p || i + Next[i - a] >= p)
        {
            if(i >= p) p = i;
            while(p < n && p - i < m && S[p] == T[p - i])
                ++ p;
            extend[i] = p - i;
            a = i;
        }
        else extend[i] = Next[i - a];
    }
}
int main(void)
{
    int k;
    cin >> k;
    while(k --)
    {
        mp.clear();
        scanf("%s %s",a , b);

        strcpy(S , b) , n = strlen(S);
        lena = strlen(a) , lenb = strlen(b);
        
        for(int i = 0 ; i < lena ; ++ i)
            mp[a[i]] = 'a' + i;
        for(int i = 0 ; i <= lenb ; ++ i)
            T[i] = mp[b[i]];

        m = strlen(T);
        getExtend();

        int i;
        for(i = 0 ; i < n ; ++ i)
            if(i + extend[i] >= n && i >= extend[i])
                break;
                
        for(int j = 0; j < i ; ++ j)
            printf("%c",S[j]);
        for(int j = 0 ; j < i ; ++ j)
            printf("%c",mp[S[j]]);
        puts("");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章