Codeforces Round #170 (Div. 2)Tire--KMP

Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems.

You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.

A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.

String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.

Input

The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.

Output

Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.

Sample test(s)
Input
5
threehorses
goodsubstrings
secret
primematrix
beautifulyear
Output
j
Input
4
aa
bdefghijklmn
opqrstuvwxyz
c
Output
ab
首先是KMP做法:
/*此题用KMP算法做的思路就是枚举1到3个字母的串(必定会出现一个未出现的子串)证明如下:
  因为至多30个单词,每个单词至多20个字母。枚举子串头子串尾,每个单词最多可能有C(20)(2)(组合数)=190.最多不可能超过6000个串(哪怕算上单字母的)
  26的三次方>6000.所以得证。。
  只需将枚举的串作为模板,与所给单词进行匹配。如果出现匹配数==模板长度。就找到了。
  PS:可以将所有单词合成一个串。。
	  此题模板多但长度短,而文本串又很长,根据刘汝佳的《算法竞赛入门经典训练指南》。适合用AC自动机做。。。。
	  表示还不会AC自动机。。。
*/#include<iostream>
using namespace std;
void getfail(char *P,int *next)
{
	int m=strlen(P);
	next[0]=0;
	next[1]=0;
	for(int i=1;i<m;i++)
	{
		int j=next[i];
		while(j&&P[i]!=P[j])j=next[j];
		next[i+1]=P[i]==P[j]?j+1:0;
	}
}//
int find(char*T,char *P,int *f)
{
	int ans=1;
	int n=strlen(T);
	int m=strlen(P);
	getfail(P,f);
	int j=0;
	for(int i=0;i<n;i++)
	{
		while(j&&P[j]!=T[i])j=f[j];
		if(P[j]==T[i])j++;
		if(j==m){ans=0;break;}
	}
	return ans;
}
char sto[31][21];
char aa[27],bb[27],cc[27],dd[27];
int a,b,c,d;
char ans[5];

int main()
{
	int digitans=1;
	int f[10];
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>sto[i];//sto是原有的单词
	int flag=0;
	for(int i=1;i<=26;i++)aa[i]=i-1+'a';
	for(int i=0;i<=26;i++)bb[i]=i==0?0:i-1+'a';
	for(int i=0;i<=26;i++)cc[i]=i==0?0:i-1+'a';
	//for(digitans=1;!flag;digitans++)
	for(int i=1;i<=26&&!flag;i++)
	{
		ans[0]=aa[i];
		int pflag=1;
		for(int j=0;j<n&&!flag;j++)
		{
			if(!find(sto[j],ans,f))pflag=0;/
		}
		if(pflag)flag=1;
	}
	for(int i=1;i<=26&&!flag;i++)
	{
		for(int j=1;j<=26&&!flag;j++)
		{
			ans[0]=aa[i];
			ans[1]=bb[j];
			int pflag=1;
			for(int j=0;j<n&&!flag;j++)
			{
				if(!find(sto[j],ans,f))pflag=0;/
			}
			if(pflag)flag=1;
		}
	}
	for(int i=1;i<=26&&!flag;i++)
	{
		for(int j=1;j<=26&&!flag;j++)
		{
			for(int k=1;k<=26&&!flag;k++)
			{
				ans[0]=aa[i];
				ans[1]=bb[j];
				ans[2]=cc[k];
				int pflag=1;
				for(int j=0;j<n&&!flag;j++)
				{
					if(!find(sto[j],ans,f))pflag=0;/
				}
				if(pflag)flag=1;
			}
		}
	}
	cout<<ans<<endl;//97
	return 0;
}
 
                                      
接下来是Tire做法:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
#define maxn 60008
struct Tire
{
	int ch[maxn][26];
	int sz;
	Tire()
	{
		sz=1;
		memset(ch,0,sizeof(ch));
	}
	int idx(char c)
	{
		return c-'a';
	}
	void insert(char *s)
	{
		int len=strlen(s);
		int u=0;
		for(int i=0;i<len;i++)
		{
			int c=idx(s[i]);
			if(!ch[u][c])
			{
				ch[u][c]=sz++;
			}
			u=ch[u][c];
		}
	}
	bool find(char *s)
	{
		int len=strlen(s);
		int u=0;
		for(int i=0;i<len;i++)
		{
			int c=idx(s[i]);
			if(!ch[u][c])
			{
				return 1;
			}
			u=ch[u][c];
		}
		return 0;
	}
}wa;
int main()
{
	int n;
	char A[28];
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			cin>>A;
			int len=strlen(A);
			for(int j=0;j<len;j++)
			{
				for(int k=0;k<len;k++)
				{
					char B[24];
					int m=0;
					for(int v=j;v<=k;v++)
					{
						B[m++]=A[v];
					}
					B[m]='\0';
					//这里用不用给B加一个尾巴\0呢?
					wa.insert(B);
				}
			}
		}
		bool flag=false;
		//接下来找找看一个字母的有没有
		for(int i=0;i<26;i++)
		{
			if(!wa.ch[0][i])
			{
				flag=true;
				char cc=i+'a';
				cout<<cc<<endl;
				break;
			}
		}
		if(!flag)
		{
			for(int i=0;i<26;i++)
			{
				for(int j=0;j<26;j++)
				{
					char C[3];
					C[0]=i+'a';
					C[1]=j+'a';
					C[2]='\0';
					if(wa.find(C))
					{
						flag=true;
						cout<<C<<endl;
						break;
					}
				}
				if(flag)break;
			}
		}
		if(!flag)
		{
			for(int i=0;i<26;i++)
			{
				for(int j=0;j<26;j++)
				{
					for(int k=0;k<26;k++)
					{
						char C[4];
						C[0]=i+'a';
						C[1]=j+'a';
						C[2]=k+'a';
						C[3]='\0';
						if(wa.find(C))
						{
							flag=true;
							cout<<C<<endl;
							break;
						}
					}
					if(flag)break;
				}
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值