HDU 4300 Clairewd’s message exkmp

一、内容

 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

二、思路

  • 第一行给定了一个明文对应暗文的表,如第一个位置明文是‘a’对应的字符是什么。叫求从一个位置划分开来,前面一段是暗文,后面一段是明文使的明文和暗文(转化成明文后) 相等的最少字符串长度。
  • 那么我们可以把S(给定的第二行的串) 全部看作是暗文,然后我们转化为明文串T。
  • 现在我们求S的后缀 和 T 的最长公共前缀。 因为S的后缀就是明文,T的前缀也是明文,那么只需要求出来一个最长的公共前缀即可。
  • 还有由于要划分的位置是对半分,那么2个串不能有重叠部分。 故 代码中 i >= j

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5, M = 27;
int t, n, ans;
char mat[M], S[N], T[N];
int h[M], ne[N], ex[N]; 
void getNext() {
	int i = 0, po;
	ne[0] = n;
	while (i + 1 < n && T[i] == T[i + 1]) i++;
	ne[1] = i; po = 1;
	for (int i = 2; i < n; i++) {
		int p = ne[po] + po, len = ne[i - po];
		if (i + len < p) ne[i] = len;
		else {
			int j = p - i;  
			if (j < 0) j = 0;
			while (i + j < n && T[i + j] == T[j]) j++;
			ne[i] = j; po = i;
		}
	} 
}
void exkmp() {
	getNext();
	int i = 0, po;
	while (i < n && S[i] == T[i]) i++;
	ex[0] = i; po = 0;
	for (int i = 1; i < n; i++) {
		int p = ex[po] + po, len = ne[i - po];
		if (i + len < p) ex[i] = len;
		else {
			int j = p - i;
			if (j < 0) j = 0;
			while (i + j < n && S[i + j] == T[j]) j++;
			ex[i] = j; po = i;
			if (j > ans && j >= n - i && i >= j) ans = j; //注意必须是完整的后缀 且i的位置要大于j才能分开 
		}
	}
} 
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%s%s", mat, S); n = strlen(S);
		for (int i = 0; i < 26; i++) h[mat[i] - 'a'] = i; 
		//将S转化为明文 
		for (int i = 0; i < n; i++) {
			T[i] = h[S[i] - 'a'] + 'a';
		} 
		ans = 0;
		exkmp();
		for (int i = 0; i < n - ans; i++) printf("%c", S[i]);
		for (int i = 0; i < n - ans; i++) printf("%c", char(h[S[i] - 'a'] + 'a')); puts("");
	}
	return 0;
}
发布了497 篇原创文章 · 获赞 513 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104699355