第一道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的更多相关文章

  1. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  2. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  3. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  4. POJ 2001 Shortest Prefixes 【Trie树】

    <题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...

  5. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  6. 第一课trie 树 POJ 2001

    最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...

  7. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  8. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  9. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

随机推荐

  1. HDU 1521 排列组合 (母函数)

    题目链接 Problem Description 有n种物品,并且知道每种物品的数量.要求从中选出m件物品的排列数.例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB&qu ...

  2. 概述sysfs文件系统【转】

    转自:http://blog.csdn.net/npy_lp/article/details/78933292 内核源码:linux-2.6.38.8.tar.bz2 目标平台:ARM体系结构 sys ...

  3. option和 usb-serial驱动基本区别

    option.c This driver exists because the "normal" serial driver doesn't work too well   wit ...

  4. (转)什么是CDC类(Communication Device Class)

    全文地址:http://justmei.blog.163.com/blog/static/1160998532010321112522467/ 什么是CDC类 (Communication Devic ...

  5. 作为一个新手的Oracle(DBA)学习笔记【转】

    一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...

  6. 匿名函数、lambda表达式

    匿名函数 func = lambda x: y #x是形参,y是返回值 键字lambda表示匿名函数,冒号前面的x表示函数参数,冒号后面的y表示匿名函数的返回值. 例1:返回列表中长度大于等于3的元素 ...

  7. Ubuntu连接多台Ubuntu server的问题

    如果您用的是虚拟机上安装的几个Ubuntu server进行IP配置 要注意以下几点: <1>虚拟机上安装完成Ubuntu server 默认的网络连接方式是NAT ,应该改成桥接网卡 ( ...

  8. ASP.NET Core 2.0 MVC 发布部署--------- Ubuntun 16.04 X64 具体操作

    .Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...

  9. mknod命令

    mknod - make block or character special filesmknod [OPTION]... NAME TYPE [MAJOR MINOR]    option 有用的 ...

  10. 当想把html element里面的text提取出来可以试着用正则

    var a='123<span>456</span><span class="active">789</span>'; a.repl ...