Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11390 Accepted Submission(s): 4025
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.
题意:
比较难理解,给定两组字符串,第一组只有26个字符表对应明文中a,b,c,d....z可以转换第1个,第2个...第26个字符变成密文,
第二组字符串是给定的密文+明文,明文可能不完整(缺失或没有),叫你补充完整整个密文+明文是最短的;
其实把求出密文的个数即可;
对密文+明文翻译 对照密文+明文 明文+密文 原函数问主函数,翻译后的问文本串求出最长的密文长度 (kmpj即可或者hash);
具体看代码
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int maxn=1e5+5; int n; int nextt[maxn]; char ma[maxn]; string name1,name2,name3; void cul(){ nextt[0]=-1; int i=0,j=-1; int len=name3.size(); while(i<len){ if(j==-1||nextt[i]==nextt[j]){ i++,j++; nextt[i]=j; } else j=nextt[j]; } } int kmp(int len){ int i=0,j=0; if(len%2==1) i=len/2+1; else i=len/2; while(i<len&&j<len){ if(j==-1||name3[j]==name2[i]){ i++,j++; } else j=nextt[j]; } return j; } int main(){ ios::sync_with_stdio(false); cin>>n; while(n--){ cin>>name1>>name2; name3.clear(); int len=name1.size(); for(int i=0;i<len;i++){ ma[name1[i]]=i; } len=name2.size(); for(int i=0;i<len;i++){ name3+=ma[name2[i]]+'a'; } //转化之后的 cul(); int ee=kmp(len); //返回密文的长度 if(ee*2==len) cout <<name2 << endl; else{ cout << name2; int tt=len-ee; //还剩下的长度 for(int i=ee;i<tt;i++){ cout << name3[i]; } cout << endl; } } return 0; }
#include <iostream> #include <cstring> #include <algorithm> #define ull unsigned long long //溢出会自动取余 using namespace std; const int maxn=1e5+5; int n,base=233; ull p[maxn],Hash1[maxn],Hash2[maxn]; char ma[maxn]; string name1,name2,name3; void init(){ p[0]=1; for(int i=1;i<maxn;i++) p[i]=p[i-1]*base; } ull get(int left,int right,ull g[]){ return g[right]-g[left-1]*p[right-left+1]; } int main(){ ios::sync_with_stdio(false); init(); cin>>n; while(n--){ cin>>name1>>name2; for(int i=0;i<26;i++){ ma[name1[i]-'a']='a'+i; } int len=name2.size(); name2=" "+name2; Hash1[0]=0; for(int i=1;i<=len;i++) Hash1[i]=Hash1[i-1]*base+(name2[i]); //原串Hash; Hash2[0]=0; for(int i=1;i<=len;i++) Hash2[i]=Hash2[i-1]*base+(ma[name2[i]-'a']); int ans=len; for(int i=len;i<=len*2;i++){ if(i&1) continue; int temp=i/2; int length=len-temp; ull aa=get(len-length+1,len,Hash1); ull bb=get(1,length,Hash2); if(aa==bb){ ans=temp; break; } } for(int i=1;i<=ans;i++) cout << name2[i]; for(int i=1;i<=ans;i++) cout << ma[name2[i]-'a']; cout << endl; } return 0; }