Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7464 Accepted Submission(s): 2776
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
1:哈希
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; #define ull unsigned long long const int maxm = 100005; const ull base = 163; char a[maxm], b[maxm]; ull Hash[2][maxm] = { 0 }, p[maxm], c[maxm]; ull get(int l, int r, ull g[]) { return g[r] - g[l - 1] * p[r - l + 1]; } void work() { int len = strlen(b + 1); int ans = len; for (int i = 0;i < 26;i++) c[a[i] - 'a'] = i; for (int i = 1;i <= len;i++) { Hash[0][i] = Hash[0][i - 1] * base + (b[i] - 'a'); Hash[1][i] = Hash[1][i - 1] * base + c[b[i] - 'a']; } for (int i = len;i < len * 2;i++) { if (i & 1) continue; ull s1 = get(1, len - i / 2, Hash[1]); ull s2 = get(i / 2 + 1, len, Hash[0]); if (s1 == s2) { ans = i / 2; break; } } for (int i = 1;i <= ans;i++) printf("%c", b[i]); for (int i = 1;i <= ans;i++) printf("%c", 'a' + c[b[i] - 'a']); printf("\n"); } int main() { int t; p[0] = 1; for (int i = 1;i <= 100000;i++) p[i] = p[i - 1] * base; scanf("%d", &t); while (t--) { scanf("%s%s", a, b + 1); work(); } return 0; }
2:Next数组应用#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; const int maxm = 100005; char a[maxm], b[maxm], p[maxm]; int Next[maxm]; void get(char str[],int n) { int i = 0, j = -1; Next[0] = -1; while (i < n) { if (j == -1 || str[i] == str[j]) Next[++i] = ++j; while (j != -1 && str[i] != str[j]) j = Next[j]; } } int main() { int n, i, j, k, sum, t; scanf("%d", &t); while (t--) { scanf("%s%s", a, b); for (i = 0;a[i] != '\0';i++) p[a[i] - 'a'] = 'a' + i; n = strlen(b); int mid = (n + 1) / 2; for (i = 0;i < mid;i++) a[i] = p[b[i] - 'a']; a[i] = '*';a[i + 1] = '\0'; strcat(a, b + i); get(a, n + 1); int len = n - Next[n + 1]; for (i = 0;i < len;i++) printf("%c", b[i]); for (i = 0;i < len;i++) printf("%c", p[b[i] - 'a']); printf("\n"); } return 0; }