HDOJ 4300 —— 拓展KMP

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.
 

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
 

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 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;
}



你可能感兴趣的:(KMP,ACM)