1. 程式人生 > >hdu4300 Clairewd’s message (擴充套件KMP)

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;
}