第一道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. MyBatis笔记之配置输出日志并打印输出SQL语句

    1. 引入Log4J的Maven依赖: <dependency> <groupId>log4j</groupId> <artifactId>log4j& ...

  2. NASA: A Closer View of the Moon(近距离观察月球)

    Posted to Twitter by @Astro_Alex, European Space Agency astronaut Alexander Gerst, this image shows ...

  3. 用于启动 Windows Phone 8 内置应用的 URI 方案

    本主题列出了可用于启动内置应用的 URI 方案.许多内置于 Windows Phone 的应用,都可以通过调用 LaunchUriAsync(Uri) 和传入一个使用与要启动应用相关的方案的 URI, ...

  4. ASP.NET Core 2.0 MVC 发布部署--------- SUSE 16 Linux Enterprise Server 12 SP2 X64 具体操作

    .Net Core 部署到 SUSE 16 Linux Enterprise Server 12 SP2 64 位中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk ...

  5. java基础8 构造函数和构造代码块

    一.构造函数 1 构造函数的作用 给对应的对象进行初始化. 2 构造函数的格式 修饰符 函数名(形式参数){ //函数名就是类名 函数体 } 举例说明: class Perosn{ private i ...

  6. linux 下配置文件目录/etc/sysconfig

    /etc/sysconfig/目录详解 2010-06-19 11:12 6693人阅读 评论(1) 收藏 举报 桌面环境debugging防火墙serviceunix语言 /etc/sysconfi ...

  7. linux 端口设置结构体 struc

      目录(?)[-] 一 输入模式 三输出模式 四控制模式 六特殊的控制字符 字符 TIME和MIN值 通过shell访问终端模式 终端速度 其他函数 使用termios结构的密码程序   termi ...

  8. 5.rabbitmq 主题

    1.生产者 #!/usr/bin/env python import pika import sys connection = pika.BlockingConnection(pika.Connect ...

  9. Gitlab部署及汉化操作

    一.简介 GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. GitLab拥有与Github类似的功能 ...

  10. DB2和Oracle中Date比较