HDU - 4300(扩展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

题意:

首先t组输入

第一行给你26个字符,即字符串S1, 表示字符的加密方式,即a~z中的第i个字符加密后变为为S1[i]

第二行一个截取的字符串,这个字符串的前部分是情报的密文,后面是情报明文的一部分(因为发送情报的人发现自己被侦察了就不发了),

让你求这个最短的完整信息

 

重点:

密文的长度一定满足temp>=(len+1)/2  这样才符合题意 ,

将该信息解密后的前缀解密后的后缀最大匹配  表示最长的可能明文,注意这个最长的可能明文长度不能超过len-temp

问题就改成了两个字符串求前缀后缀最大匹配(扩展kmp)

 

#include
#include
#include
#include
#include
using namespace std;
const int maxn=2e5+7;
typedef long long ll;
const int MOD=1e5+7;
int code[100];//将密文转化为明文
char convert(char c)
{
    return code[c-'a']+'a';
}
char infostr[maxn];
char codestr[maxn];
int net[maxn];//存放info的next数组
int maxpp[maxn];//最长后缀匹配前缀数组
void get_nextval(char *s,int* net)
{
    int ls=strlen(s);
    int j=0,k=-1;
    net[0]=-1;
    while(j

 

你可能感兴趣的:(#,KMP)