HDU 1867 A + B for you again KMP

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1867


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef __int64 LL;
const int N=100005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

char s1[N],s2[N];
int len,next[2*N];

void getnext(char *p,int wlen)
{
    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])
            j++,i++;
        else
            j=next[j];
    }
    return j;
}

int main()
{
    int ca=0,i,j,T;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int len1,len2,t1,t2;
        len1=strlen(s1);
        len2=strlen(s2);

        getnext(s2,len2);
        t1=kmp(s1,s2);//s2模式串
        getnext(s1,len1);
        t2=kmp(s2,s1);//s1模式串
        if(t1==t2)
        {
            if(strcmp(s1,s2)<0)
                printf("%s%s\n",s1,s2+t1);
            else
                printf("%s%s\n",s2,s1+t1);
            continue;
        }

        if(t1<t2)
            printf("%s%s\n",s2,s1+t2);
        else
            printf("%s%s\n",s1,s2+t1);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值