【链接】
poj2001
【题目大意】
求这些单词无歧义的最短前缀,详见题意。
【解题报告】
Trie模板水题,不用多解释。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxv=20005,maxn=1005,maxm=25,maxt=26;
int n;
char c[maxn][maxm];
struct Tire
{
int son[maxv][maxt],len;
bool vis[maxv];
int Getid(char ch) {return ch-'a';}
void Insert(char *s)
{
int now=0;
for (int i=1; s[i]; i++)
{
int p=Getid(s[i]);
if (!son[now][p]) son[now][p]=++len,vis[len]=1; else vis[son[now][p]]=0;
now=son[now][p];
}
}
int Getpre(char *s)
{
int now=0,i;
for (i=1; s[i]; i++)
{
int p=Getid(s[i]);
if (son[now][p]) now=son[now][p]; else break;
if (vis[now]) break;
}
if (!s[i]) i--;
return i;
}
}tr;
int main()
{
freopen("2001.in","r",stdin);
freopen("2001.out","w",stdout);
n=tr.len=0;
memset(tr.vis,0,sizeof(tr.vis));
while (scanf("%s\n",c[++n]+1)!=EOF) tr.Insert(c[n]); n--;
for (int i=1; i<=n; i++)
{
printf("%s ",c[i]+1);
int len=tr.Getpre(c[i]);
for (int j=1; j<=len; j++) putchar(c[i][j]); putchar(10);
}
return 0;
}