POJ2001 Shortest Prefixes trie树模板

10 篇文章 0 订阅

http://poj.org/problem?id=2001


题目大意:现在人们喜欢用缩写,比如carbon可以缩写为carb,但不能缩写为car。因为有car这个准确的单词。给你n个单词(n<=1000),每个单词长度不超过20。求出每个单词的最短缩写。


题意:先建立trie树,树上节点增加一个计数器。然后对于每个单词用trie树查找,到第一次出线计数器为1时停止输出即可。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
//const double eps=1e-7;
//const double INF=1e50;
//const double pi=acos(-1);

#define N 1005
#define M 20005

char st[N][30];

struct tree
{
    char ch;
    vector<int> c;
    int ci;
}p[M];

int check(int q,char cha)
{
    for (int i=0;i<(int)p[q].c.size();i++)
    if (p[p[q].c[i]].ch==cha) return p[q].c[i];

    return -1;
}

int main()
{
    freopen("a","r",stdin);

    int i,j,l,q,num=0,n=1;
    while(gets(st[n]))
    {
        l=strlen(st[n]);

        q=0;
        p[0].ci=10000;
        for (i=0;i<l;i++)
        {
            int an=check(q,st[n][i]);
            if (an>-1) q=an;
            else
            {
                num++;
                p[q].c.push_back(num);

                p[num].ch=st[n][i];
                p[num].ci=0;
                q=num;
            }

            p[q].ci++;
        }

        n++;
    }

    for (i=1;i<n;i++)
    {
        printf("%s ",st[i]);
        l=strlen(st[i]);

        q=0;
        for (j=0;j<l;j++)
        {
            int an=check(q,st[i][j]);
            if (j==(l-1) || p[an].ci==1)
            {
                for (int k=0;k<=j;k++) printf("%c",st[i][k]);
                printf("\n");
                break;
            }
            else q=an;
        }
    }

    return 0;
}



#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
//const double eps=1e-7;
//const double INF=1e50;
//const double pi=acos(-1);

#define N 1005
#define M 20005

char st[N][30];

struct tree
{
    char ch;
    vector<int> c;
    int ci;
}p[M];

int check(int q,char cha)
{
    for (int i=0;i<(int)p[q].c.size();i++)
    if (p[p[q].c[i]].ch==cha) return p[q].c[i];

    return -1;
}

int main()
{
    freopen("a","r",stdin);

    int i,j,l,q,num=0,n=1;
    while(gets(st[n]))
    {
        l=strlen(st[n]);

        q=0;
        for (i=0;i<l;i++)
        {
            p[q].ci++;

            int an=check(q,st[n][i]);
            if (an>-1) q=an;
            else
            {
                num++;
                p[q].c.push_back(num);

                p[num].ch=st[n][i];
                p[num].ci=0;
                q=num;
            }
        }
        p[q].ci++;//这里要加因为最后一个字符虽然访问了,但树节点还没开始访问,

        n++;
    }

    for (i=1;i<n;i++)
    {
        printf("%s ",st[i]);
        l=strlen(st[i]);

        q=0;
        for (j=0;j<l;j++)
        {
            int an=check(q,st[i][j]);
            if (j==(l-1) || p[an].ci==1)
            {
                for (int k=0;k<=j;k++) printf("%c",st[i][k]);
                printf("\n");
                break;
            }
            else q=an;
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值