HDU2594 Simpsons’ Hidden Talents

12 篇文章 0 订阅
5 篇文章 0 订阅

Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
Output
Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.
Sample Input
clinton
homer
riemann
marjorie
Sample Output
0
rie 3
题意:求两串字符连接起来后前缀后缀公共元素的最长长度。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;

const int N = 1000002;
int nextt[N];
char S[N], T[N];
int slen, tlen,len;
int sum;

void getnextt()
{
    int j,k;
    j=0;
    k=-1;
    nextt[0]=-1;
    while(j < len)
        if(k == -1 || T[j] == T[k])
        {
            j++;k++;
            nextt[j]=k;

        }
        else
        {
            k=nextt[k];
        }
}

int main()
{

    int TT;
    int i, cc,n;
    while(~scanf("%s%s",T,S))
    {

        slen = strlen(S);
        tlen = strlen(T);
        for(int i=tlen;i<tlen+slen;i++)
            T[i]=S[i-tlen];
        len=tlen+slen;
        getnextt();
        while(nextt[len]>tlen||nextt[len]>slen)//这里很重要,一开始没写wa了好多发。如果得到的答案超过字符串a,b本身那就要缩小len的范围。令L1=next[len],得到的前L1项组成的字符串,其后缀一定与原字符串的后缀是相同的,所以L2=next[next[len]],L2也一定是原字符串的前缀后缀公共元素长度
            len=nextt[len];
        if(nextt[len])
        {
            for(int i=0;i<nextt[len];i++)
                printf("%c",T[i]);
            printf(" ");
        }

        printf("%d\n",nextt[len]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值