现在的位置: 首页 > 综合 > 正文

【hdu 2594】Simpsons’ Hidden Talents

2017年04月25日 ⁄ 综合 ⁄ 共 1395字 ⁄ 字号 评论关闭

http://acm.hdu.edu.cn/showproblem.php?pid=2594

和上一道一样

//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
char s1[100010],s2[100010];
int next[100010];
int l1,l2,l;
/////////////////////////////////////////////////
void get_next()
{
	int i=0,j=-1; next[0]=-1;
	while(i<l)
	{
		if(j==-1||s1[i]==s1[j]) next[++i]=++j;
		else j=next[j];
	}
}
/////////////////////////////////////////////////
void input()
{
	l1=strlen(s1);
	l2=strlen(s2);
	l=l1+l2;
	strcat(s1,s2);
}
void solve()
{
	get_next();
	int ans=0;
	for(int i=l;i>0;i=next[i])
	{
		if(i<=l1&&i<=l2)
		{
			ans=i;
			break;
		}
	}
	rep(i,0,ans-1) putchar(s1[i]);
	if(ans) putchar(' ');
	printf("%d\n",ans);
}
/////////////////////////////////////////////////
int main()
{
    #ifndef _TEST
    freopen("std.in","r",stdin); freopen("std.out","w",stdout);
    #endif
    while(~scanf("%s%s",s1,s2))
    input(),solve();
    return 0;
}

抱歉!评论已关闭.