HDU 4300 Clairewd’s message (kmp数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300点击打开链接

Clairewd’s message

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


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

求循环节长度后判断即可

#include <bits/stdc++.h>
using namespace std;
map<char,char> mingmi,miming;
char t[111111];
int nnext[111111];
int n;
void getnext()
{
	int i=n/2,j=0;
	if(n&1)
		i++;
	nnext[0]=-1;
	while(i<n)
	{
		if(j==-1||t[i]==miming[t[j]])
		{
			i++;
			j++;
			nnext[i]=j;
		}
		else
		{
			j=nnext[j];
		}
	}
}
int  main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        mingmi.clear();
        miming.clear();
        for(int i=0;i<26;i++)
        {
            char mid;
            scanf(" %c",&mid);
            mingmi['a'+i]=mid;
            miming[mid]='a'+i;
        }
        scanf(" %s",t);
        n=strlen(t);
        getnext();
        int len=n-nnext[n];
        //cout << len << endl;
        for(int i=0;i<n;i++)
            printf("%c",t[i]);
		if(n/2!=len)
        for(int i=n-len;i<len;i++)
            printf("%c",miming[t[i]]);
        printf("\n");
    }
}



猜你喜欢

转载自blog.csdn.net/xuejye/article/details/79251847