POJ2001[Shortest Prefixes]--Trie

【链接】
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值