HDU 4300 (KMP)
来源:互联网 发布:北京宝库在线网络 编辑:程序博客网 时间:2024/06/06 08:52
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5014 Accepted Submission(s): 1900
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
2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
Sample Output
abcdabcdqwertabcde
题意:给出一个字符的映射表来加密字符,然后有一个串由密文+对应的明文构成,
但是后面的明文有缺失,然后求出长度最小的符合要求的串.
先把原串T都解密一遍,假设新串是S,相当于要求S的前缀和T后缀匹配的长度要尽量
大.这就是next数组的含义了,但是显然这个匹配的前缀长度需要小于等于n/2的,所
以如果这个条件不满足就沿着next一直匹配.这里可以通过求S+T而避免扩栈KMP.
边界的地方容易搞错.
#include <cstring>#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cmath>using namespace std;#define maxn 211111char P[maxn], T[maxn];int n, m;#define next Nextint next[maxn];char change[33], ss[33];void get_next (char *p) {int t;t = next[0] = -1;int j = 0, m = strlen (p);while (j < m) {if (t < 0 || p[j] == p[t]) {//匹配j++, t++;next[j] = t;}else //失配t = next[t];}}void Change (char *s) { for (int i = 0; i < 26; i++) change[ss[i]-'a'] = 'a'+i; int n = strlen (s); for (int i = 0; i < n; i++) { s[i] = change[s[i]-'a']; } return ;}int main () { //freopen ("in.txt", "r", stdin); int t; scanf ("%d", &t);while (t--) { scanf ("%s%s", ss, T); memset (P, 0, sizeof P); strcpy (P, T); n = strlen (T); m = n*2; Change (P); strcat (P, T); get_next (P); int j = m, mid = n/2; while (next[j] > 0 && next[j] > mid) j = next[j]; int len = n-next[j]; for (int i = 0; i < len; i++) printf ("%c", T[i]); for (int i = len; i < len*2; i++) printf ("%c", change[T[i-len]-'a']); cout << "\n";}return 0;}/*7qwertyuiopasdfghjklzxcvbnmqwertqwertyuiopasdfghjklzxcvbnmqwertaqwertyuiopasdfghjklzxcvbnmqabcdefghijklmnopqrstuvwxyzaaaaabcdefghijklmnopqrstuvwxyzaaaabcdefghijklmnopqrstuvwxyzaaabcdefghijklmnopqrstuvwxyza*/
0 0
- hdu 4300 扩展KMP
- HDU 4300 (KMP)
- HDU 4300(KMP)
- HDU 4300Clairewd’s message 扩展kmp || kmp
- hdu 4300 Clairewd’s message 扩展kmp
- hdu - 4300 - Clairewd’s message- kmp
- HDU 4300 Clairewd’s message(拓展KMP)
- hdu 4300 Clairewd’s message(扩展KMP)
- KMP hdu-4300 Clairewd’s message
- HDU 4300 Clairewd’s message KMP
- hdu 4300 Clairewd’s message(KMP)
- hdu 4300 Clairewd’s message(KMP)
- hdu 4300 Clairewd’s message (KMP)
- HDU 4300 Clairewd’s message(扩展KMP)
- hdu 4300 Clairewd’s message KMP算法
- hdu 4300 Clairewd’s message (KMP)
- hdu 4300 Clairewd’s message (kmp)
- hdu 4300 Clairewd’s message (扩展kmp)
- 【java】KDTree,实现个java版本,留着日后可能用得上
- 自学Java步骤
- Java并发编程:线程池的使用
- wifidog源码分析 - 初始化阶段
- Android之圆角矩形
- HDU 4300 (KMP)
- PHP实现文件上传与下载
- 第10、11周项目2:存储班长信息的学生类(2)
- 数据结构实验之链表六:有序链表的建立
- java RTTI和反射
- Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记
- wifidog源码分析 - wifidog原理
- SildingMenu在androidstudio上面的安装
- android局域网内通过socket通信