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
Author
BUPT
Source
题意
给一段字母表,其中s[i]代表第i个字母的密文是s[i],然后一段字符串是密文+明文,明文可能没有或者是残缺的或者是完整的,密文是完整的,求密文最短的条件下输出密文+明文的完整串
可以将原来的字符串整个加密,然后后面的明文就会变成密文,前面的密文就被破坏了,变成了一段乱码,
这个时候,把原串和加密的串拼接起来,求next数组,得到的最大的(前缀和后缀相等)的长度就是明文的长度,其中,这个长度不能超过原串的一半。如果超过一般,那就:ans=Next[ans]
代码
//============================================================================
// Name : tree.cpp
// Author : Qihan
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
#define pi acos(-1.0)
#define Lowbit(x) (x & (-x))
#define lson l,mid,rt << 1
#define rson mid + 1,r,rt << 1 | 1
#define inf 0x3f3f3f3f
#define maxn (100000 + 10)
#define maxm (3000000 + 10)
using namespace std;
typedef long long int LLI;
typedef pair<int,int> PII;
int Next[maxn * 2];
char b[maxn * 2];
void getNext(int len) {
int j = 0, k = -1;
Next[0] = -1;
while(j < len) {
if(k == -1 || b[j] == b[k]) {
j ++,k ++;
Next[j] = k;
} else k = Next[k];
}
}
char str[maxn];
char str1[maxn];
char a[maxn];
int main() {
// freopen("/home/qihan/Documents/in","r",stdin);
// freopen("/home/qihan/Documents/out","w",stdout);
int t;
scanf("%d",&t);
while (t--) {
scanf("%s%s", str, b);
int len = strlen(b);
for (int i = 0; i < len; i++) {
a[i] = str[b[i] - 'a'];
}
for(int i = 0;i < 26;i ++){
int x = str[i] - 'a';
str1[x] = 'a' + i;
}
a[len] = '\0';
int lim = len / 2;
strcat(b, a);
getNext(len * 2);
int ans = Next[len * 2];
while (ans > lim) {
ans = Next[ans];
}
for (int i = 0; i < len - ans; i++) {
printf("%c", b[i]);
}
for (int i = 0; i < len - ans; i++) {
printf("%c", str1[b[i] - 'a']);
}
printf("\n");
}
return 0;
}