HDOJ 4300 —— 拓展KMP
来源:互联网 发布:中国便秘人群数据分析 编辑:程序博客网 时间:2024/05/22 19:26
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2780 Accepted Submission(s): 1074
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
Author
BUPT
Source
2012 Multi-University Training Contest 1
Recommend
zhuyuanchen520
本题有两种做法:比较快的是直接暴搜,31MS。另一种是拓展KMP,比较慢。
1、暴搜:
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1//#define mid ((l + r) >> 1)#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 100000 + 50;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x3f3f3f3f;const int IMIN = 0x80000000;const double E = 2.718281828;#define eps 1e-8#define DEBUG 1#define mod 100000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;// #pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];char rechange[200];char str[200];char anstr[MAXN];char str2[MAXN];int main(){ int t; cin >> t; while(t--) { scanf("%s%s" , str , str2);// clr(rechange , 0); for(int i = 0 ; i < 26 ; i++) { rechange[str[i] - 'a'] = i + 'a'; } rechange[26] = 0;// for(int i = 0 ; i < 26 ; i++)// {// printf("%c " , 'a' + i);// cout << change[i] << ' ' ;// cout << "re" << ' ' << rechange[i] << endl;// } memcpy(anstr , str2 , sizeof(str2)); int len = strlen(str2); for(int i = 0 ; i < len ; i++)str2[i] = rechange[str2[i] - 'a']; int i; for(i = ((len + 1)>> 1) ; i < len ; i++) { int x = i; for(int j = 0 ; x < len ; j++ , x++) { if(anstr[x] != str2[j]) { break; } } if(x == len) { for(int j = 0 ; j < i ; j++) { putchar(anstr[j]); } for(int j = 0 ; j < i ; j++) { putchar(str2[j]); } putchar('\n'); break; } } if(i == len) { printf("%s%s\n" , anstr , str2); } } return 0;}2、拓展KMP:
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <utility>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1//#define mid ((l + r) >> 1)#define mk make_pair#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 100000 + 50;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x3f3f3f3f;const int IMIN = 0x80000000;const double E = 2.718281828;#define eps 1e-8#define DEBUG 1#define mod 100000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;// #pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];char word[MAXN],change[26] , ming[26] , keep[MAXN];int wlen , next[MAXN];void get_next(char *p){ int j = 0 , k = -1; next[0] = -1; while(j < wlen) { if(k == -1 || p[j] == p[k]) { j++ , k++; next[j] = k; } else k = next[k]; }}int kmp(char *text , char *word){ int i = 0 , j = 0 , tlen = strlen(text); while(i < tlen) { if(j == -1 || text[i] == word[j]) { i++ , j++; } else j = next[j]; } return j;}int main(){ int t; cin >> t; while(t--) { scanf("%s%s" ,ming , word); printf("%s" , word); FOR(i , 0 , 26)change[ming[i] - 'a'] = i + 'a'; wlen = strlen(word); strcpy(keep , word + (wlen + 1) / 2); FOR(i , 0 , wlen)word[i] = change[word[i] - 'a']; get_next(word); int t = kmp(keep , word); int p = wlen - t; word[p] = '\0'; printf("%s\n" , word + t); } return 0;}
- HDOJ 4300 —— 拓展KMP
- hdu4333——拓展kmp
- hdu4300——拓展kmp
- 【拓展kmp】
- 拓展kmp
- 拓展KMP
- HDOJ 1711 Number Sequence ——KMP
- kmp&&拓展kmp
- HDU 4300 Clairewd’s message(拓展KMP)
- hdu 4513 吉哥系列故事——完美队形II(拓展KMP算法)
- Best Reward +拓展KMP
- HDU 4763 拓展KMP
- kmp算法及其拓展
- 拓展kmp算法总结
- 拓展kmp模板
- kmp板子及其拓展
- hdu 6153 拓展kmp
- 拓展KMP算法详解
- SSIS【Foreach 循环容器_Foreach Item 枚举器】(循环读取数据库)
- 极大似然和最小平方误差等价关系
- Linux内核探讨-- 第四章
- iphone通知中心里的"精品推荐"的删除方法
- 数组中和为N的组合
- HDOJ 4300 —— 拓展KMP
- 我的博客
- 深入浅出C++11(2) -- C++核心语言的扩充
- ora-28001
- 得到数组的最后一个数的趣味实现
- 查找算法总结(一)
- EasyMock 使用方法与原理剖析
- JDBC
- 利用StateListDrawable当EditText获得,失去焦点时分别设置其内容的不同颜色