沉迷WOW又颓了两天orz,暴雪爸爸要在国服出月卡了...这是要我好好学习吗?赶紧来刷题了...

OJ:http://poj.org/problem?id=2001

题目大意是求所有字符串里每一个字符串最短唯一前缀(就是说要可以通过这个前缀确定唯一的字符串,也可以理解为这个前缀是某个字符串独有的,求最短的就OK了)

 #include<iostream>
 #include<cstring>
 #include<cstdio>
 using namespace std;
 ;
 +;
 struct Trie{
     int ch[maxnode][sigma_size];
     int val[maxnode];
     int sz;
     Trie(){sz=;memset(ch[],,]));memset(val,,sizeof(val));}
     int idx(char c){return c-'a';};

     void insert(char *s){
         /*起点是根节点*/,n=strlen(s);
         val[]++;
         ;i<n;i++){
             int x=idx(s[i]);
             if(!ch[u][x]){
                 memset(ch[sz],,sizeof(ch[sz]));
                 val[sz]++;/*不是最终的节点 附加值是0*/
                 ch[u][x]=sz++;/*连接起来*/
             }else val[ch[u][x]]++;
             u=ch[u][x];
         }
         /*插入完成之后赋值*/
     }
     void Judge(char *s){
         /*从起点开始查找*/,n=strlen(s);
         ;i<n;i++){
             ) break;
             printf("%c",s[i]);
             int x=idx(s[i]);
             u=ch[u][x];
         }
         return;
     }
 };
 ][];
 Trie book;
 int main()
 {
     ,i=;
     )){//不能用scanf
         N++;
         book.insert(input[i-]);
     }
     ;i<N;i++){
         printf("%s ",input[i]);
         book.Judge(input[i]);
         printf("\n");
     }
     ;
 }

POJ2001-Shortest Prefixes-Trie树应用的更多相关文章

  1. poj2001 Shortest Prefixes (trie树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  2. poj2001 Shortest Prefixes(字典树)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

  3. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

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

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

  5. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  6. poj 2001 Shortest Prefixes trie入门

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

  7. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  8. POJ 2001 Shortest Prefixes (Trie)

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

  9. POJ 2001 Shortest Prefixes(字典树)

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

  10. poj 2001 Shortest Prefixes(字典树)

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

随机推荐

  1. 【原】sql 将某列拼成一个字符串

    SQL Server中,写存储过程,时常会碰到这样一个需求:从某个表中取某一列,然后需要将这一列数据以某种形式拼成一个字符串,以供后面使用,下面这种方法能够实现此需求. --取说明书模块枚举,结果格式 ...

  2. tarjan算法 POJ3177-Redundant Paths

    参考资料传送门 http://blog.csdn.net/lyy289065406/article/details/6762370 http://blog.csdn.net/lyy289065406/ ...

  3. Android三-AsyncTask

    基础介绍:http://blog.csdn.net/liuhe688/article/details/6532519 扩展知识:http://blog.csdn.net/pipisky2006/art ...

  4. 黄聪:C# 开发Chrome内核浏览器(WebKit.net)

    WebKit.net是对WebKit的.Net封装,使用它.net程序可以非常方便的集成和使用webkit作为加载网页的容器.这里介绍一下怎么用它来显示一个网页这样的一个最简单的功能. 第一步: 下载 ...

  5. C#多线程编程(转)

    一.使用线程的理由 1.可以使用线程将代码同其他代码隔离,提高应用程序的可靠性. 2.可以使用线程来简化编码. 3.可以使用线程来实现并发执行. 二.基本知识 1.进程与线程:进程作为操作系统执行程序 ...

  6. css针对(各大浏览器、各版本)调兼容

    ie6\ie7\firefox之下各自识别的CSS符号 #1 { color: #333; } /* firefox */ * html #1 { color: #666; } /* IE6 */ * ...

  7. mac打开.caj格式文件

    以为用mac后使用会变得更方便些,写毕设时终于派上用场,可惜啊,mac竟然打不开.caj文件,这意味着什么?相信所有在做毕设的小伙伴们都能懂其中的凄凉.特别是硕士或博士的论文,你得从知网上下上百篇的文 ...

  8. loadrunner破解

    如果报的violation的错误,就下载hp.lr即deletelicense.exe软件运行即可以清楚注册表,然后再用管理员运行loadrunner再注册就可以了.

  9. C++学习2

    命名空间(Namespace)主要为了避免命名冲突,其关键字为namespace 在多人代码整合过程中常用到: namespace Li{ //小李的变量声明 ; } namespace Han{ / ...

  10. Java中数字操作

    public static void main(String[] args) throws Exception { { //Math函数的四舍五入,注意负数的时候小数位<=0.5都会被舍去,&g ...