POJ 2001 Shortest Prefixes
来源:互联网 发布:java if else语句举例 编辑:程序博客网 时间:2024/06/01 19:20
字典树模板题,没啥好说的。只是为了记录下模板。
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int sonnum=26,base='a';struct Trie{ int x; int num; bool terminal; struct Trie *son[sonnum];};int allocp;Trie tarray[10005];Trie *NewTrie(){ Trie *temp=&tarray[allocp++]; temp->num=1; temp->x=0; temp->terminal=false; for(int i=0;i<sonnum;i++) temp->son[i]=NULL; return temp;}void Insert(Trie *pnt,char *s){ Trie *temp =pnt; for(int i=0;s[i]!='\0';i++) { if(temp->son[s[i]-base]==NULL) { temp->son[s[i]-base]=NewTrie(); temp->son[s[i]-base]->x=temp->x+1; } else temp->son[s[i]-base]->num++; temp=temp->son[s[i]-base]; } temp->terminal=true;}void Delete(Trie *pnt){ for(int i=0;i<allocp;i++) { tarray[i].num=1; tarray[i].terminal=false; for(int j=0;j<sonnum;j++) tarray[i].son[j]=NULL; } allocp=0;}int Find(Trie*pnt,char *s){ Trie*temp=pnt; for(int i=0;s[i]!='\0';i++) { if(temp->son[s[i]-base]->num>1)temp=temp->son[s[i]-base]; else return temp->son[s[i]-base]->x-1; } return temp->x-1;}int main(){ char a[1001][30]; allocp=0; Trie *root=NewTrie(); int num=0; while(scanf("%s",a[num])!=EOF) { Insert(root,a[num]); num++; } for(int i=0;i<num;i++) { printf("%s ",a[i]); int l=Find(root,a[i]); for(int j=0;j<=l;j++) printf("%c",a[i][j]); printf("\n"); } return 0;}
- 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 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ-2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- HDU 1068 Girls and Boys (二分图匹配---最大独立集)
- hadoop集群搭建
- annotation与httpclient -- HttpClient使用Annotation和Proxy
- Android开发 自定义ProgressBar
- ORA-01552: 非系统表空间 'TEMP' 不能使用系统回退段
- POJ 2001 Shortest Prefixes
- FLV文件格式解析
- 51job
- Coding the Matrix: Week 7 Orthogonalization 学习笔记
- 网络编程1
- android 项目更改包名的方法
- bsdiff for ubuntu
- 关于一个int *ptr=(int *)(&a+1)问题的探讨
- struts2的执行流程、工作原理