poj 2001 trie
第一道trie
还需要写题来建立自己的代码习惯。
#include <cstdio>
#include <vector>
#include <algorithm>
#define maxn 20010
using namespace std; struct node {
char v;
int sz;
bool isword, mark;
node *son[], *pre;
}pool[maxn], *tail=pool, *null=pool; char temp[];
struct Trie {
node *root;
vector<node*> stk;
node* newnode( node *p, char v ) {
node *nd = ++tail;
nd->sz = ;
nd->v = v;
nd->isword = nd->mark = false;
nd->pre = p;
for( int i=; i<; i++ ) nd->son[i] = null;
return nd;
}
void insert( const char *str ) {
if( !root ) root=newnode(null,'^');
node *nd=root;
while() {
if( *str ) {
if( nd->son[*str-'a']==null ) nd->son[*str-'a']=newnode(nd,*str-'a');
nd->sz++;
nd = nd->son[*str-'a'];
str++;
} else {
nd->isword = true;
stk.push_back( nd );
return;
}
}
}
void make_mark( node *nd ) {
if( nd->isword ) {
nd->mark = true;
for( int i=; i<; i++ )
if( nd->son[i]!=null ) make_mark(nd->son[i]);
} else if( nd->sz== ) {
nd->mark = true;
} else {
for( int i=; i<; i++ )
if( nd->son[i]!=null ) make_mark(nd->son[i]);
}
}
char *get( node *bt ) {
while( !bt->mark ) bt=bt->pre;
int i;
for( i=; bt!=root; i++,bt=bt->pre )
temp[i] = bt->v+'a';
temp[i] = ;
reverse( temp, temp+i );
return temp;
}
void print( node *nd ) {
fprintf( stderr, "nd %d ch %c mark %d\n", nd-pool, nd->v+'a', nd->mark );
for( int i=; i<; i++ )
if( nd->son[i]!=null ) print(nd->son[i]);
}
}T; void pt( char *st ) {
fprintf( stderr, "%s\n", st );
}
char str[][];
int main() {
int i;
for( i=; ; i++ ) {
if( scanf( "%s", str[i] )!= ) {
i--;
break;
}
T.insert( str[i] );
}
for( int i=; i<; i++ )
if( T.root->son[i]!=null )
T.make_mark( T.root->son[i] );
for( int t=; t<=i; t++ )
printf( "%s %s\n", str[t], T.get(T.stk[t]) );
}
poj 2001 trie的更多相关文章
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- POJ 2001 Shortest Prefixes 【Trie树】
<题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- 第一课trie 树 POJ 2001
最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
随机推荐
- Xcode 获取本地IP
// // // #define MAXADDRS 32 extern char *ip_names[MAXADDRS]; void InitAddresses(); void GetIPAddres ...
- webgote的例子(4)Sql注入(SelectGET)
SQL Injection (Select/GET) 本章内容 (查询显示中要注意的错误) 这里面我们看一下 movie的数值,选择表单中的当我们选择的二个的时候 move的值也变成了第二个,选择表单 ...
- 38 - 网络编程-socketserver
目录 1 socket编程弊端 2 SocketServer模块 2.1 服务器类 2.2 Mixin类 2.3 RequestHandlerClass是啥 2.4 编程接口 3 实现EchoServ ...
- java程序out of memory【转】
相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...
- UVALive 5760 Alice and Bob
题意是黑板上有n个数\(S_i\).每次操作可以把其中一个数减1或者将两个数合并为一个数.一个数变为0时,则不能再对其操作. 思路是发现最大的可操作次数为\( \sum S_i\)+(n - 1).\ ...
- 第一天开始使用Oracle
上半年虽然已经学习了Oracle,但是基本上实验课都没怎么实践过,感觉自己之前过得太水了! 在我的印象里,Oracle 的难度相当于工程师的建设一个亚洲最大的医院一样,也如医生的99%失败率的手术: ...
- Linux下通过源码编译安装程序(configure/make/make install的作用,然后在/etc/profile文件里修改PATH环境变量)
一.程序的组成部分 Linux下程序大都是由以下几部分组成: 二进制文件:也就是可以运行的程序文件 库文件:就是通常我们见到的lib目录下的文件 配置文件:这个不必多说,都知道 帮助文档:通常是我们在 ...
- 1.第一个hello word
1.生产者 #!/usr/bin/env python import pika connection = pika.BlockingConnection(pika.ConnectionParamete ...
- MyBatis3-与Spring MVC 4集成
继前一篇的例子http://www.cnblogs.com/EasonJim/p/7052388.html,已经集成了Spring框架,现在将改造成Spring MVC的项目,并实现如下功能: 1.不 ...
- POJ2528 Mayor's posters(线段树染色问题+离散化)
题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报 ...