hdu4300 Clairewd’s message【next数组应用】

时间:2023-03-09 13:10:15
hdu4300 Clairewd’s message【next数组应用】

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9512    Accepted Submission(s): 3458

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
Recommend
zhuyuanchen520

题意:

给定26个字母对应的加密规则和一串字符串。字符串的前半部分是密文,后半部分是对应的原文,但是原文可能只有前面一部分。

现在要你找到最短的密文,输出密文+原文

思路:

其实就是让字符串的后缀和前缀尽可能多的匹配(也就是$next[len]$),这样后面原文补全的部分就比较少。

对应的密文的长度就是$len-next[len]$。因为题意说密文和原文不能重叠,所以需要特判$next[len]$超过一半的情况。

加密规则用map存一下就好了。

刚开始数组开小了10倍T了半天。后来忘记考虑重叠的情况了,这里改掉就AC了。

 #include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e5 + ;
char str[maxn];
map<char, char>mp;
int nxt[maxn]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == mp[s[k]]){
++k;++j;
if(s[j] != mp[s[k]]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} bool kmp(char *s, char *t)
{
getnxt(s);
int slen = strlen(s), tlen = strlen(t);
int i = , j = ;
while(i < slen && j < tlen){
if(j == - || s[i] == t[j]){
j++;
i++;
}
else{
j = nxt[j];
}
}
if(j == tlen){
return true;
}
else{
return false;
}
} int main()
{
int t;
scanf("%d", &t);
while(t--){
mp.clear();
getchar();
for(int i = ; i < ; i++){
char c;
scanf("%c", &c);
mp[c] = 'a' + i;
} scanf("%s", str);
getnxt(str);
int len = strlen(str);
int ans = len - nxt[len];
if(nxt[len] > (len + ) / ){
ans = (len + ) / ;
} for(int i = ; i < ans; i++){
printf("%c", str[i]);
}
for(int i = ; i < ans; i++){
printf("%c", mp[str[i]]);
}
printf("\n");
}
return ;
}