1. 程式人生 > >hdu4300 Clairewd’s message(字符串匹配 hash)

hdu4300 Clairewd’s message(字符串匹配 hash)

printf 算法 不能 not accepted hole 不知道 member borde

Clairewd’s message

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


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 2012 Multi-University Training Contest 1

Recommend zhuyuanchen520 | We have carefully selected several similar problems for you: 4302 4301 4303 4304 4308 關於字符串匹配(Hash算法), 可以看一下https://www.cnblogs.com/zyf0163/p/4806951.html

題目大意:

是有一份文件,前面是密文,後面是原文,但那個人接到這個文件後不知道中間從哪裏開始是原文,所以你要幫忙還原一下,如果後面原文比密文少,你就將它補全, 第一行是密文轉換格式,例如第二個樣例表示將q翻譯成a,w翻譯成b。

思路:

我們只要先把密文都翻譯成明文,然後去比較原來的字符串的後綴和翻譯之後的字符串前綴的最長匹配長度就行(註:最長匹配的長度不能超過原長的一半)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 typedef unsigned long long ull;
 8 const int N = 1000005;
 9 const int seed = 163;
10 char a[N], b[N];
11 int c[N];
12 
13 int T;
14 ull p[N];
15 
16 vector<ull>v1, v2;
17 
18 void init(){
19     p[0] = 1;
20     for(int i=1; i<=100000; i++)
21         p[i] = p[i-1] * seed;
22 }
23 
24 ull rkhash(char *s, int id){
25     ull Hash = 0;
26     for(int i=1; s[i]; i++){
27         if(id == 1){
28             Hash = (Hash*seed + s[i]-a);
29             v1.push_back(Hash);
30         }else if(id == 2){
31             Hash = (Hash*seed + c[s[i]-a]);
32             v2.push_back(Hash);
33         }
34     }
35     return Hash;
36 }
37 
38 ull get(int l, int r, vector<ull> &g){
39     return g[r] - g[l-1] * p[r-l+1];
40 }
41 
42 void work(){
43     v1.clear(); v2.clear();
44     v1.push_back(0); v2.push_back(0);
45     for(int i=0; i<26; i++) c[a[i]-a] = i;
46     rkhash(b, 1); rkhash(b, 2);
47     int n = strlen(b+1);
48     int ans = n;
49     for(int i=n; i<n*2; i++){
50         if(i & 1) continue;
51         int tmp = i / 2;
52         int len = n-tmp;
53         ull s1 = get(1, len, v2);
54         ull s2 = get(n-len+1, n, v1);
55         if(s1 == s2){
56             ans = tmp;
57             break;
58         }
59     }
60     for(int i=1; i<=ans; i++) printf("%c", b[i]);
61     for(int i=1; i<=ans; i++) printf("%c", c[b[i]-a]+a);
62     puts("");
63 }
64 
65 int main(){
66     scanf("%d", &T);
67     init();
68     
69     while(T--){
70         scanf("%s%s", a, b+1);
71         work();
72     }
73     
74 
75     return 0;
76 }

hdu4300 Clairewd’s message(字符串匹配 hash)