poj 3080

题意:找n 个字符串中最长的公共子川 若存在多个最常的则输出 字典序最小的

思路:kmp  擦 本来没难度  结果因为字典序 错过了n次

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
char str[22][66];
int next[66];
int L;
char s[66];
void getnext()
{
	int i,j;
	next[1]=0;
	next[0]=1;
	i=1;j=0;
	int l=strlen(s);
	while(i<l)
	{
		if(j==0||s[i]==s[j])
		{
			i++;j++;
			next[i]=j;
		}
		else j=next[j];
	}
}
bool fu(int x)
{
	int l=strlen(str[x]+1);
	int ls=strlen(s+1);
	int i,j;
	i=1;j=1;
	while(i<=ls&&j<=l)
	{
		if(s[i]==str[x][j])
		{
			i++;j++;
		}
		else 
			if(i==1)
			{
				j++;
			}
		else i=next[i];
	}
	if(i>ls) return true;
	return false;
}
int main()
{
	char ss[66];
	int i,j,k;
	int f;
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		f=0;
		char end[]="no significant commonalities";
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%s",str[i]+1);
		L=strlen(str[0]+1);
		for(j=3;j<=L;j++)
		{
			for(i=1;i+j-1<=L;i++)
			{
				strncpy(s+1,str[0]+i,j);
				s[1+j]='';
				getnext();
				for(k=1;k<n;k++)
					if(!fu(k))
						break;
					if(k>=n)
					{
						if(f==1&&strcmp(ss,s+1)>0)
						  strcpy(ss,s+1);
						else
						  strcpy(ss,s+1);
						f=1;
					
						
					}
				
			}
		}
		if(!f)
			printf("%s
",end);
		else printf("%s
",ss);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/zhangdashuai/p/3788432.html