首页 > 其他分享 >POJ 2001 Shortest Prefixes(字典树)

POJ 2001 Shortest Prefixes(字典树)

时间:2023-04-13 21:34:46浏览次数:53  
标签:node cnt int Prefixes flag 2001 POJ include root


题目地址:POJ 2001

考察的字典树,利用的是建树时将每一个点只要走过就累加。最后从根节点开始遍历,当遍历到只有1次走过的时候,就说明这个地方是最短的独立前缀。然后记录下长度,输出即可。

代码如下:


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>

using namespace std;
char s[1100][30];
struct node
{
    int flag;
    node *next[30];
};
node *newnode()
{
    int i;
    node *p;
    p=new node;
    for(i=0;i<26;i++)
    {
        p->next[i]=NULL;
    }
    p->flag=0;
    return p;
}
void insert1(node *root, char *s)
{
    int i, len=strlen(s), x;
    node *p=root;
    for(i=0;i<len;i++)
    {
        x=s[i]-'a';
        if(p->next[x]==NULL)
            p->next[x]=newnode();
        p=p->next[x];
        p->flag++;
    }
}
int seach(node *root, char *s)
{
    int i, len=strlen(s), x, pos=len;
    node *p=root;
    for(i=0;i<len-1;i++)
    {
        x=s[i]-'a';
        p=p->next[x];
        if(p->flag==1)
        {
            pos=i+1;
            break;
        }
    }
    return pos;
}
int main()
{
    node *root;
    root =newnode();
    int cnt=0, i, j, k;
    while(scanf("%s",s[cnt])!=EOF)
    {
        insert1(root,s[cnt]);
        cnt++;
    }
    for(i=0;i<cnt;i++)
    {
        printf("%s ",s[i]);
        k=seach(root,s[i]);
        for(j=0;j<k;j++)
        {
            printf("%c",s[i][j]);
        }
        printf("\n");
    }
    return 0;
}



标签:node,cnt,int,Prefixes,flag,2001,POJ,include,root
From: https://blog.51cto.com/u_16070138/6188416

相关文章

  • POJ 1182 食物链(种类并查集)
    题目地址:POJ1182一道很经典的种类并查集的题目。利用互相之间的关系来进行权值的维护。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#in......
  • POJ 1703 Find them, Catch them(种类并查集)
    题目地址:POJ1703种类并查集水题。代码如下:#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<queue>#include<map>#includ......
  • POJ 1988 Cube Stacking (种类并查集)
    题目地址:POJ1988   这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能马上输出,这样的话在合并的时候就要所有的结点值都要算出来,但是经过路径压缩之后,没办法全部都处理到,如果不压缩妥妥的TLE。。于是看了看网上的题解。才发现自己是多......
  • Java常用实体类介绍:POJO、Domain、DO、DTO、VO
    POJOPOJO是PlainOldJavaObject的简称,它指的是一个没有限制或要求下的纯平对象。POJO用于表示没有任何框架或技术限制的纯数据对象。在Java开发中,POJO通常用于简化复杂对象和降低对象的耦合度,是面向对象编程中"高内聚、低耦合"设计思想的体现。示例代码:@Datapublic......
  • UVa 706 / POJ 1102 LCD Display (模拟)
    706-LCDDisplayTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=647http://poj.org/problem?id=1102Afriendofyouhasjustboughtanewcomputer.Untilno......
  • UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想
    757-GoneFishingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698http://poj.org/problem?id=1042Johnisgoingonafishingtrip.Hehas h hoursavailable( ),andther......
  • UVa 113 / POJ 2109 Power of Cryptography (使用double处理大整数&泰勒公式与误差分
    113-PowerofCryptographyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49http://poj.org/problem?id=2109题意:给出n和p,求出 ,但是p可以很大()如何存储p?不用大数可不可以?先看看double......
  • UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
    443-HumbleNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384http://poj.org/problem?id=2247Anumberwhoseonlyprimefactorsare2,3,5or7isc......
  • poj 3090 Visible Lattice Points
     #include<iostream>#include<algorithm>usingnamespacestd;constintM=1e6;intvis[M+4],P[M+4],cnt;intfi[M+4];voidshai(inttop){ cnt=0; fi[1]=1; for(inti=2;i<=top;i++){ if(vis[i]==0){ P[++cnt]=i; fi[i]=i-1;......
  • Mondriaan's Dream 【POJ2411】 题解
    Mondriaan'sDream【POJ2411】题解——ByZy注:原题中的\(h,w\)在本文中使用\(n,m\)代替。一.题意分析:题目要求给定一个一定大小的矩形棋盘,求出使用\(1\times2\)大小的木条填充一共有多少种方案。读题,发现数据范围\((1\leh,w\le11)\),考虑使用状压DP算法,于是......