1. 程式人生 > >hdu__KMP演算法模板題【持續更新中】

hdu__KMP演算法模板題【持續更新中】

hdu 1867 A + B for you again

                                                                  Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                           Total Submission(s): 5272    Accepted Submission(s): 1325
                                                                                     連結:Click me!
Problem Description Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions. Input For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty. Output Print the ultimate string by the book. Sample Input asdf sdfg asdf ghjk Sample Output asdfg asdfghjk

題意:

給定字串a,b,輸出兩個字串連線起來的最小字典序,有個條件,如果字串s1的某個字首與s2的某個字尾相同,那麼可以把字首和字尾合併起來,也就是是要輸出字首或者字尾中的一個即可。

#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         int T;for(scanf("%d",&T);T--;)
const int maxn = 100000+5;
char a[maxn],b[maxn];
int Next[maxn];
void getNext(char x[],int m,int next[])
{
    int i,j;
    j = next[0] = -1, i = 0;
    while(i < m)
    {
        while(-1 != j && x[i] != x[j]) j = next[j];
        ++i, ++j;
        if(x[i] == x[j]) next[i] = next[j];
        else next[i] = j;
    }
}
int KMP(char s[],int slen,char p[],int plen)
{
    int i,j,ans = 0;
    getNext(p,plen,Next);
    i = j = 0;
    while(i < slen)
    {
        while(-1 != j && s[i] != p[j]) j = Next[j];
        ++i,++j;
    }
    return (j == -1) ? 0 : j;
}
int main()
{
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    while(~scanf("%s %s",a,b))
    {
        int lab = 0,lba = 0,t;
        int alen = strlen(a), blen = strlen(b);
        t = min(alen,blen);
        lab = KMP(a,alen,b,blen);
        lba = KMP(b,blen,a,alen);
        if(lab > lba)
        {
            a[alen-lab] = '\0';
            printf("%s%s\n",a,b);
        }
        else if(lab < lba)
        {
            b[blen-lba] = '\0';
            printf("%s%s\n",b,a);
        }
        else if(lab == 0)
        {
            string str1(a);str1 += b;
            string str2(b);str2 += a;
            printf("%s\n",min(str1,str2).c_str());
        }
        else
        {
            char t1;
            t1 = a[alen-lab];
            a[alen-lab] = '\0';
            string str1(a);
            str1 += b;
            a[alen-lab] = t1;
            b[blen-lba] = '\0';
            string str2(b);
            str2 += a;
            printf("%s\n",min(str1,str2).c_str());
        }
    }
    return 0;
}