HDOJ4300Clairewd’s message【扩展KMP】

Clairewd’s message

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

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<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAX=1000010;
int next[MAX];
int extend[MAX];
int MIN(int a,int b){
	return a<b?a:b;
}
char table[30];
void getNext(char *T){
	int i,lenT=strlen(T);
	next[0]=lenT;
	for(i=0;i<lenT-1&&T[i]==table[T[i+1]-'a'];++i);
	next[1]=i;
	int a=1;
	for(int k=2;k<lenT;++k){
		int p=a+next[a]-1,L=next[k-a];
		if((k+L-1)>=p){
			int j=(p-k+1)>0?(p-k+1):0;
			while(k+j<lenT&&T[j]==table[T[k+j]-'a'])++j;
			next[k]=j;a=k;
		}
		else {
			next[k]=L;
		}
	}
}
char str[MAX];
char zhuanhuan[30];
int main()
{
	int t,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%s",table);
		scanf("%s",str);
		for(i=0;i<26;++i){
			zhuanhuan[table[i]-'a']='a'+i;
		}
		memset(next,0,sizeof(next));
		getNext(str);
		int len=strlen(str);
		for(i=(strlen(str)+1)/2;i<len;++i){
			 if(next[i]==len-i)break;
		}
		for(j=0;j<i*2;++j){
			if(j>=i){
				printf("%c",zhuanhuan[str[j-i]-'a']);
			}
			else {
				printf("%c",str[j]);
			}
		}
		printf("\n");
	}
	return 0;
}
    原文作者:KMP算法
    原文地址: https://blog.csdn.net/r1986799047/article/details/47760033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞