HDU 4300 Clairewd’s message 字符串hash

来源:互联网 发布:java的api文档怎么用 编辑:程序博客网 时间:2024/06/15 22:56

Clairewd’s message

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


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
2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
 

Sample Output
abcdabcdqwertabcde
 

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 
 

题意好难理解,读题简直就是哭瞎了,,我试着说一下题意

输入刚开始是一个T,再然后第一串字符是密文,即只有26个字符表对应明文中a,b,c,d....z可以转换这串中的第一个,第二个...第26个字符,以变成密文,

第二串是密文和明文合在一起的,明文(试着理解下)可能有好多丢失的,让我们补全明文且字符串最短。

一开始用的纯暴力,超时;

改成字符哈希,对密文+明文串hash一下,比较的时候时间复杂度就相当于O(1),这样就可以ac了


ac代码;

#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <map>#include <algorithm>#define ull unsigned long longusing namespace std;const int maxn=100000+10;const int base=163;char mi[30];char sed[maxn];ull hash1[maxn],hash2[maxn],p[maxn];void INIT(){p[0]=1;for(int i=1;i<maxn;i++) p[i]=p[i-1]*base;}ull get(int l,int r,ull g[]){/*获得l-r这个区间内的字符串hash值*/ return g[r]-g[l-1]*p[r-l+1];}int main(){int T;INIT();scanf("%d",&T);while(T--){scanf("%s",mi);scanf("%s",sed+1);/*使用map容器,对应一下那个密文*/ map<char,char> mmp;for(int i=0;i<26;i++){mmp[mi[i]]='a'+i;}int cnt=strlen(sed+1);hash1[0]=hash2[0]=0;/*进行hash*/ for(int i=1;i<=cnt;i++){hash1[i]=hash1[i-1]*base+(sed[i]-'a');hash2[i]=hash2[i-1]*base+(mmp[sed[i]]-'a');}int ans=cnt;/*答案串的长度必须是偶数且长度最短为cnt*/ for(int i=cnt;i<2*cnt;i++){if(i & 1) continue ;int tmp=i/2;int len=cnt-tmp;ull s1=get(1,len,hash2);ull s2=get(cnt-len+1,cnt,hash1);if(s1 == s2){ans=tmp;break ;}//printf("%llu %llu\n",s1,s2);}for(int i=1;i<=ans;i++) printf("%c",sed[i]);for(int i=1;i<=ans;i++) printf("%c",mmp[sed[i]]);printf("\n");}return 0;} 


这个题咏扩展kmp也可以解,暂时还不会,待解。