HDU 4300 Ciairewd's message(kmp应用)

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8379    Accepted Submission(s): 3071


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
 
  
2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
 

Sample Output
 
  
abcdabcdqwertabcde
 

Author
BUPT
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4302  4301  4303  4304  4308 
#include<string.h>
#include<stdio.h>
const int maxn=2e5+7;
char table[maxn];
char text[maxn];
int Next[maxn],pass[maxn];
/*
³¬Ê±¡£¡£¡£¡£
*/
void getNext(char* t)
{
    int k=-1,i=0,len=strlen(t);
    Next[0]=-1;

    while(i < len+1)
    {
        if(k==-1 || t[i]==t[k])
            Next[++i] = ++k;
        else
            k = Next[k];
    }
}

//void handle(string& s)
//{
//    int len=s.size();
//    if(len&1)  s=s.substr(0,len/2+1)+'*';
//    else  s=s.substr(0,len/2)+'*';
//}

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",table,text);
        int i;
        int N=strlen(text);
        int Mid=(N+1)/2;
        for(i=0; table[i]; i++)
        {
            pass[ table[i]-'a' ] = i+'a';
        }
        for(i=0;i<Mid;i++) table[i]=pass[text[i]-'a'];
        table[i]='*',table[i+1]=0;
        strcat(table,text+i);
        getNext(table);
        Mid=N-Next[strlen(table)-1]-1;

        for( i=0;i<Mid;i++)
        {
            table[i]=text[i];
            table[i+Mid]=pass[table[i]-'a'];
        }
        table[i+Mid]=0;
        printf("%s\n",table);
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/80153845