hdu 2594 next数组简单运用

点击打开链接

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio> 
using namespace std;
const int M =111000;
char s1[M],s2[M];
int l1,l2;
int fail[M];

// 题意: 求与s2后缀相等的s1的最长前缀
// 连接接s1,s2 求出 next[l1+l2]中不大于min(l1,l2)的前缀即可 
void Get()
{
	int i=0,k=-1;
	fail[0]=-1;
	while(i<l1+l2)
	{
		if(k==-1||s1[i]==s1[k])
		{
			fail[i+1]=k+1;
			i++;
			k++;
			
		}
		else
		{
			k=fail[k];
		}
	}
}

int main()
{
	while(scanf("%s",s1)!=EOF)
	{
		scanf("%s",s2);
		l1=strlen(s1);
		l2=strlen(s2);
		strcat(s1,s2);
		Get();
		int len=l1+l2;
		
		while(fail[len]>l1 || fail[len]>l2) //相同的一段不能越串 
		len=fail[len];
		
		len=fail[len]; //找到合法的len 
		
		for(int i=0;i<len;i++)
		{
			cout<<s1[i];
		}
		if(len) 
		cout<<" ";
		cout<<len<<endl;
	}	
	
	return 0;
	
} 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值