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.
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.
T<= 100 ;
n<= 100000;
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了。
1 #include<iostream> 2 //#include<bits/stdc++.h> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<algorithm> 8 #include<queue> 9 #include<vector> 10 #include<set> 11 #include<climits> 12 #include<map> 13 using namespace std; 14 typedef long long LL; 15 typedef unsigned long long ull; 16 #define pi 3.1415926535 17 #define inf 0x3f3f3f3f 18 19 const int maxn = 1e5 + 100; 20 char str[maxn]; 21 map<char, char>mp; 22 int nxt[maxn]; 23 24 void getnxt(char *s) 25 { 26 int len = strlen(s); 27 nxt[0] = -1; 28 int k = -1; 29 int j = 0; 30 while(j < len){ 31 if(k == -1 || s[j] == mp[s[k]]){ 32 ++k;++j; 33 if(s[j] != mp[s[k]]){ 34 nxt[j] = k; 35 } 36 else{ 37 nxt[j] = nxt[k]; 38 } 39 } 40 else{ 41 k = nxt[k]; 42 } 43 } 44 } 45 46 bool kmp(char *s, char *t) 47 { 48 getnxt(s); 49 int slen = strlen(s), tlen = strlen(t); 50 int i = 0, j = 0; 51 while(i < slen && j < tlen){ 52 if(j == -1 || s[i] == t[j]){ 53 j++; 54 i++; 55 } 56 else{ 57 j = nxt[j]; 58 } 59 } 60 if(j == tlen){ 61 return true; 62 } 63 else{ 64 return false; 65 } 66 } 67 68 int main() 69 { 70 int t; 71 scanf("%d", &t); 72 while(t--){ 73 mp.clear(); 74 getchar(); 75 for(int i = 0; i < 26; i++){ 76 char c; 77 scanf("%c", &c); 78 mp[c] = 'a' + i; 79 } 80 81 scanf("%s", str); 82 getnxt(str); 83 int len = strlen(str); 84 int ans = len - nxt[len]; 85 if(nxt[len] > (len + 1) / 2){ 86 ans = (len + 1) / 2; 87 } 88 89 for(int i = 0; i < ans; i++){ 90 printf("%c", str[i]); 91 } 92 for(int i = 0; i < ans; i++){ 93 printf("%c", mp[str[i]]); 94 } 95 printf("\n"); 96 } 97 return 0; 98 }