沉迷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. python学习-day11-内建函数

    python-内建函数 -int:将字符串转换为数字 a = " print(type(a),a) b = int(a) print(type(b),b) num = " v = ...

  2. SG函数模板

    这篇虽然是转载的,但代码和原文还是有出入,我认为我的代码更好些. 转载自:http://www.cnblogs.com/frog112111/p/3199780.html 最新sg模板: //MAXN ...

  3. (转)zookeeper学习记录--附browser

    转自:http://agapple.iteye.com/blog/1111377 背景 前段时间看了S4流计算引擎,里面使用到了zookeeper进行集群管理,所以也就花了点时间研究了下zookeep ...

  4. protobuf使用说明

    1..proto文件为要生成.java文件的模板文件,其中包含名称空间.文件名等信息2.cmd中进入当前目录D:\JAVA\protoc-2.5.0-win323.运行 protoc.exe --ja ...

  5. Python进阶02 文本文件的输入输出

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python具有基本的文本文件读写功能.Python的标准库提供有更丰富的读写功能 ...

  6. 注意,ruby循环体定义的变量在结束时后,变量还存在

    a = [1, 2, 3] for i in a b = 123 p i end p "b:#{b}" p i <ruby语言编程> 129页 倒数 第8行

  7. 在asp.net 中应用POST传递和接收XML文件以及参数.(转)

    使用POST方式可以向别的页面发送请求,并获取返回结果. 可以从一个页面发送POST到另一个页面,也可以在winform工程中使用HTTPRequest发送POST到一个页面.我们拿ASP.NET的a ...

  8. RDLC报表分页显示标题

    将报表以 XML的方式打开,搜索找到“详细信息” 在这个位置 <TablixRowHierarchy> <TablixMembers> <TablixMember> ...

  9. java 的Swing

    1.原来的AWT组件来自java.awt包,而AWT组件的java应用程序在不同平台的界面显示不同.但是在不同平台运用Swing开发的应用程序,就可以使用同一的GUI界面. 2.SWing组件通常被称 ...

  10. Python的方法分类

    1.Python的类方法,实例方法,和静态方法 class S(object): def Test(self): print("TEST") @classmethod#类方法 de ...