POJ 2001 Shortest Prefixes(字典树)
来源:互联网 发布:particleillusion mac 编辑:程序博客网 时间:2024/04/24 13:22
题目地址: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;}
2 1
- POJ 2001:Shortest Prefixes(字典树)
- poj 2001 -- Shortest Prefixes ( 字典树 )
- POJ 2001-Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ - 2001 - Shortest Prefixes (字典树!!)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- 【POJ】2001 - Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- poj 2001Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- EditText
- 01背包变形 之 hdu 2126 Buy the souvenirs
- cocos2dx 3.x Node对象update函数认识
- UVA 1401 - Remember the Word(Trie+DP)
- 黑马程序员——Java高新_Java5部分新特性
- POJ 2001 Shortest Prefixes(字典树)
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- 线程池的原理及实现
- hdu 2094 产生冠军(水题,拓扑排序)
- libc.so.6共享库恢复
- 快速Fibonacci数,矩阵法
- 设备驱动调试和移植的一般方法
- 谁drop了我库中的用户
- memcached