2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde
abcdabcd qwertabcde
本题有两种做法:比较快的是直接暴搜,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 100000007 typedef 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 100000007 typedef 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; }