hdu4300 Clairewd’s message (扩展KMP)

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

题目意思:

很有必要好好说一下,题目给出两个字符串,第一个字符串长度为26,代表一个密码的转换表,第二个字符串长度未知,由密文和明文组成并且密文在前,明文在后(也就是在某个字符之前,包括它自己全在内是密文,之后的全是明文),其中密文一定是完整的,但是明文不一定。让你求长度最短的完整的明文,最后补全第二个字符串并输出。

必须要再提一下密码的转化表,题目中给出的是密文转明文的转换表,以第二个样例为例子,qwertyuiopasdf一直到最后都是密文,转换完之后就是abcdefgh....,明白了吧。。。

思路:

假设题目中给出的密文 + 明文的串为st,长度为len

首先呢,密文和明文长度是相等的(题目中可能没有给出完整明文),那么密文的最后一个必定位于(len / 2)之后。

知道扩展kmp吧,这道题的解决方法和其中求next数组是一样的,区别就在于求的时候记得把后面的明文转换成密文再比较。

当nesxt[i] == len - i的时候就满足条件的,i从len / 2开始第一个满足条件的一定是最短的。

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 1e5 + 100;

int t;
char ss[100];
char st[maxn * 10];
char ma[300];
int nex[maxn * 10];

void Exkmp(char *st)
{
	int len = strlen(st);
	nex[0] = len;
	int po = 1, i = 0;
	while(i + 1 < len && st[i] == ss[st[i + 1] - 'a'])  //把明文转换成密文
		++ i;
	nex[1] = i;
	for(i = 2; i < len; ++ i)
	{
		if(nex[i - po] + i < nex[po] + po)
			nex[i] = nex[i - po];
		else 
		{
			int j = nex[po] + po - i;
			if(j < 0)
				j = 0;
			while(i + j < len && st[j] == ss[st[i + j] - 'a'])  //明文转换成密文
				++ j;
			nex[i] = j;
			po = i;
		}
	}
}


int main()
{
	//freopen("in.txt", "r", stdin);
	cin >> t;
	while(t --)
	{
		scanf("%s", ss);
		scanf("%s", st);
		for(int i = 0; i < 26; ++ i)
		{
			ma[ss[i]] = 'a' + i;
		}
		//memset(nex, 0, sizeof(nex));
		Exkmp(st);
		int len = strlen(st);
		int i;
		for(i = (len + 1) / 2; i < len; ++ i)  //一定从(len + 1) / 2开始
		{
			if(nex[i] == len - i)
				break;
		}
		for(int j = 0; j < 2 * i; ++ j)
		{
			if(j < i)			
				printf("%c", st[j]);
			else 
				printf("%c", ma[st[j - i]]);			
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/85715032