poj 2001 trie
第一道trie
还需要写题来建立自己的代码习惯。
1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 #define maxn 20010 5 using namespace std; 6 7 struct node { 8 char v; 9 int sz; 10 bool isword, mark; 11 node *son[26], *pre; 12 }pool[maxn], *tail=pool, *null=pool; 13 14 char temp[100]; 15 struct Trie { 16 node *root; 17 vector<node*> stk; 18 node* newnode( node *p, char v ) { 19 node *nd = ++tail; 20 nd->sz = 0; 21 nd->v = v; 22 nd->isword = nd->mark = false; 23 nd->pre = p; 24 for( int i=0; i<26; i++ ) nd->son[i] = null; 25 return nd; 26 } 27 void insert( const char *str ) { 28 if( !root ) root=newnode(null,'^'); 29 node *nd=root; 30 while(1) { 31 if( *str ) { 32 if( nd->son[*str-'a']==null ) nd->son[*str-'a']=newnode(nd,*str-'a'); 33 nd->sz++; 34 nd = nd->son[*str-'a']; 35 str++; 36 } else { 37 nd->isword = true; 38 stk.push_back( nd ); 39 return; 40 } 41 } 42 } 43 void make_mark( node *nd ) { 44 if( nd->isword ) { 45 nd->mark = true; 46 for( int i=0; i<26; i++ ) 47 if( nd->son[i]!=null ) make_mark(nd->son[i]); 48 } else if( nd->sz==1 ) { 49 nd->mark = true; 50 } else { 51 for( int i=0; i<26; i++ ) 52 if( nd->son[i]!=null ) make_mark(nd->son[i]); 53 } 54 } 55 char *get( node *bt ) { 56 while( !bt->mark ) bt=bt->pre; 57 int i; 58 for( i=0; bt!=root; i++,bt=bt->pre ) 59 temp[i] = bt->v+'a'; 60 temp[i] = 0; 61 reverse( temp, temp+i ); 62 return temp; 63 } 64 void print( node *nd ) { 65 fprintf( stderr, "nd %d ch %c mark %d\n", nd-pool, nd->v+'a', nd->mark ); 66 for( int i=0; i<26; i++ ) 67 if( nd->son[i]!=null ) print(nd->son[i]); 68 } 69 }T; 70 71 void pt( char *st ) { 72 fprintf( stderr, "%s\n", st ); 73 } 74 char str[1010][30]; 75 int main() { 76 int i; 77 for( i=0; ; i++ ) { 78 if( scanf( "%s", str[i] )!=1 ) { 79 i--; 80 break; 81 } 82 T.insert( str[i] ); 83 } 84 for( int i=0; i<26; i++ ) 85 if( T.root->son[i]!=null ) 86 T.make_mark( T.root->son[i] ); 87 for( int t=0; t<=i; t++ ) 88 printf( "%s %s\n", str[t], T.get(T.stk[t]) ); 89 }
View Code
转载于:https://www.cnblogs.com/idy002/p/4331588.html
poj 2001 trie相关推荐
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- poj 2503 Trie树
典型的Trie树, 算是复习一下字符串吧, 就是输入有点恶心,代码如下: #include <cstdio> #include <cstring> #include <a ...
- 【POJ - 2001 】Shortest Prefixes (字典树,查询重复前缀区间)
题干: A prefix of a string is a substring starting at the beginning of the given string. The prefixes ...
- poj 1816 trie树+dfs(强烈推荐,含有通配符的匹配)
这个题目花了我一晚上才调出来,其实也不难,就是在trie树上搜索给定的单词,但是麻烦的一点是考虑的情况非常多,比如模式串可能重复,或者*?连续出现,最开始都没考虑,程序敲出来直接提交一看WA,然后看d ...
- [poj 2001] Shortest Prefixes (字典树)
Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepte ...
- poj 1451(Trie)
题意:就是让你模拟手机输入单词.具体就是给你一些单词,以及该单词被使用的频数,这个频数也是该单词前缀的使用频数,当然不同的单词有可能有相同的前缀,那么这个相同的前缀的使用频数就是这两个单词使用频数之和 ...
- Poj 2001 Shortest Prefix(字典树模板)
字典树入门题...... 只需在对应结点记录num值,找到num为1时的前缀,是唯一前缀(只是该单词的前缀),或者一直找找到该单词结尾. #include <iostream> #incl ...
- java 获取每月每一天_java 求取某一段时间内的每一天、每一月、每一年
1.求取某一段时间内的每一天 Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-01" ...
最新文章
- TOMCAT为什么打破双亲委派的类加载模型
- igbinary vs serialize vs json_encode
- php post可重复提交了,如何防止 POST 数据重复提交!
- linux 系统调用表 sys_call_table 获取方法
- 全球及中国绿色建筑产业规模现状与未来走势分析报告2022版
- Django默认用户认证系统和用户模型类
- 【华为云技术分享】【昇腾】ModelArts与Atlas 200 DK云端协同开发——行人检测Demo(提供完整Demo)
- linux 文件系统字体小,基于嵌入式Linux平台的最小文件系统的制作
- ICT学习笔记(3)IP编址
- linux 文件乱码_RedHatlinux系统虚机启动后/etc/fstab中的文件系统未能挂载上
- ASP.NET网站还是ASP.NET Web应用程序?
- 二分排序(java)
- 图像处理13:非极大值抑制
- 解析力 (2)空间采样 和 奈奎斯特
- 爱代挂前台模板5绿色版
- .Net Core 使用Swagger,且使用自定义UI(Knife4jUI)
- centos liunx升级内核
- 震动效果调研 - iOS
- 纳米器件,量子点理论文献拾遗
- 水星无线网卡UD198H Ubuntu20.04 驱动安装