HDU4300 Clairewd’s message 扩展kmp
2016-05-13 00:01
411 查看
题目链接:HDU4300
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5011 Accepted Submission(s): 1898
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
Sample Output
题意:先是一个表,代表从a到z对应的密文字母,之后一个字符串,有完整的密文和不知道完不完整的明文。我们的任务就是把明文补全然后一起输出。
题目分析:这次又做到这一题。以前是用的KMP将之翻译后匹配相同的字段。这次换一种方法用扩展KMP。首先还是要翻译回来,得到明文和将明文翻译后得到的一堆没意义的字符。这里的明文一定能和原串的不全的明文匹配,所以这一次需要用到前缀匹配,就是扩展KMP。具体参照杨雅琼的扩展KMPppt。这次的相当于一个模板放在这,以后又更好的写法再回来改。
PS:扩展的英文是extend,我全写成了extand,,,,这么多年学的英语都还给老师了
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5011 Accepted Submission(s): 1898
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
题意:先是一个表,代表从a到z对应的密文字母,之后一个字符串,有完整的密文和不知道完不完整的明文。我们的任务就是把明文补全然后一起输出。
题目分析:这次又做到这一题。以前是用的KMP将之翻译后匹配相同的字段。这次换一种方法用扩展KMP。首先还是要翻译回来,得到明文和将明文翻译后得到的一堆没意义的字符。这里的明文一定能和原串的不全的明文匹配,所以这一次需要用到前缀匹配,就是扩展KMP。具体参照杨雅琼的扩展KMPppt。这次的相当于一个模板放在这,以后又更好的写法再回来改。
PS:扩展的英文是extend,我全写成了extand,,,,这么多年学的英语都还给老师了
// // main.cpp // HDU4300(1 // // Created by teddywang on 16/5/12. // Copyright © 2016年 teddywang. All rights reserved. // #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; int T,n,len; char s[100010],t[100010],a[28]; int nexts[100010],extand[100010]; void getnexts(char *t) { int a=0; nexts[0]=len; while(a<len-1&&t[a]==t[a+1]) a++; nexts[1]=a; a=1; for(int k=2;k<len;k++) { int p=a+nexts[a]-1,L=nexts[k-a]; if(k-1+L>=p) { int j=max((p-k+1),0); while(k+j<len&&t[k+j]==t[j]) ++j; nexts[k]=j; a=k; } else nexts[k]=L; } } void ekmp(char *s,char *t) { int lens=len,lent=strlen(t),a=0; int minlen=min(lens,lent); while(a<minlen&&s[a]==t[a]) ++a; extand[0]=a; a=0; for(int k=1;k<lens;k++) { int p=a+extand[a]-1,L=nexts[k-a]; if(k-1+L>=p) { int j=max(p-k+1,0); while(k+j<lens&&j<lent&&s[k+j]==t[j]) ++j; extand[k]=j; a=k; } else extand[k]=L; } } int main() { cin>>T; while(T--) { char hash[150]; scanf("%s",a); for(int i=0;i<26;i++) hash[ a[i] ]='a'+i; scanf("%s",s); len=strlen(s); for(int i=0;i<len;i++) t[i]=hash[s[i]]; getnexts(t); ekmp(s,t); int pos=len; for(int i=0;i<len;i++) { if(i+extand[i]>=len&&i>=extand[i]) { pos=i; break; } } /*for(int i=0;i<len;i++) cout<<nexts[i]; cout<<endl; for(int i=0;i<len;i++) cout<<extand[i]; cout<<endl; */ for(int i=0;i<pos;i++) printf("%c",s[i]); for(int i=0;i<pos;i++) printf("%c",t[i]); printf("\n"); } }
相关文章推荐
- Leetcode 70. Climbing Stairs 爬楼梯 解题报告
- 图形绘制 Canvas Paint Path 详解
- flask发送邮件问题(yeah.net与Gmail设置的不同)
- photo mosaic 拼图马赛克
- leetcode@ [336] Palindrome Pairs (HashMap)
- fork wait
- Error Domain=NSOSStatusErrorDomain Code=1718449215 "The operation couldn’t be completed. (OSStatus error 1718449215.)"
- 启动hive报错:[ERROR] Terminal initialization failed; falling back to unsupported
- Message与obtainMessage()
- CodeForces 312C The Closest Pair(构造)
- Gmail迁移到Office365设置
- bunoj 29140 Taiko taiko
- Execution failed for task CapacitySms:mergeDebugResources Some file crunching failed 错误解决
- 人工智能-机器学习-数据挖掘-模式识别
- Convolutional Neural Networks at Constrained Time Cost
- WPF[调用线程无法访问此对象,因为另一个线程拥有该对象]主窗口MainWindow 创建的子线程 调用窗口线程的控件方法
- 修改git全部已提交的用户名和邮箱
- CodeForces 627B Factory Repairs
- http://blog.csdn.net/cswhale/article/details/16941281
- 【BaiduMapSDK系列】定位到我的位置