题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483

组织一棵Trie 记录每个节点有多少串经过 统计最大值

由于只出现0,1,于是建立一个字典树,每一次到达一个节点,就在这个节点加上深度,然后ans求解最大的节点附加值就可以了。注意初始化

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <cstdlib>
  7. #include <iomanip>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <map>
  11. #include <set>
  12. #include <queue>
  13. using namespace std;
  14. #define lowbit(x) (x&(-x))
  15. #define max(x,y) (x>y?x:y)
  16. #define min(x,y) (x<y?x:y)
  17. #define MAX 100000000000000000
  18. #define MOD 1000000007
  19. #define pi acos(-1.0)
  20. #define ei exp(1)
  21. #define PI 3.141592653589793238462
  22. #define INF 0x3f3f3f3f3f
  23. #define mem(a) (memset(a,0,sizeof(a)))
  24. #define maxnode 100000
  25. typedef long long ll;
  26. ll gcd(ll a,ll b){
  27. return b?gcd(b,a%b):a;
  28. }
  29. bool cmp(int x,int y)
  30. {
  31. return x>y;
  32. }
  33. const int N=;
  34. const int mod=1e9+;
  35. int n;
  36. struct Trie
  37. {
  38. int ch[maxnode][];
  39. int val[maxnode];
  40. int sz=,ans=;
  41. void init(){sz=;
  42. ans=;
  43. memset(ch[],,sizeof(ch[]));
  44. memset(val,,sizeof(val));
  45. }
  46. int idx(char c){
  47. return c-'';
  48. }
  49. void insert(char* s,int len){
  50. int u=;
  51. for(int i=;i<len;i++){
  52. int v=idx(s[i]);
  53. if(!ch[u][v]){
  54. memset(ch[sz],,sizeof(ch[sz]));
  55. ch[u][v]=sz++;
  56. }
  57. u=ch[u][v];
  58. val[u]+=(i+);
  59. ans=max(ans,val[u]);
  60. }
  61. }
  62. };
  63. struct Trie trie;
  64. char s[maxnode];
  65. int main()
  66. {
  67. int t;
  68. cin>>t;
  69. while(t--){
  70. trie.init();
  71. cin>>n;
  72. while(n--){
  73. cin>>s;
  74. trie.insert(s,strlen(s));
  75. }
  76. cout<<trie.ans<<"\n";
  77. }
  78. return ;
  79. }

UVA 11488 Hyper Prefix Sets (字典树)的更多相关文章

  1. UVA 11488 Hyper Prefix Sets (字典树)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  3. UVA 11488 Hyper Prefix Sets (Trie)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA - 11488 Hyper Prefix Sets(trie树)

    1.给n个只含0.1的串,求出这些串中前缀的最大和. 例1: 0000 0001 10101 010 结果:6(第1.2串共有000,3+3=6) 例2: 01010010101010101010 1 ...

  5. UVa 11488 - Hyper Prefix Sets

    找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...

  6. uva 11488 Hyper Prefix Sets(狂水)

    题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...

  7. HDU 11488 Hyper Prefix Sets (字符串-Trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  8. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

  9. 【leetcode】208. Implement Trie (Prefix Tree 字典树)

    A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...

随机推荐

  1. 20171228 C#值类型和引用类型

    public class RefPoint //定义的引用类型 { public int x; public RefPoint(int x) { this.x = x; } } public stru ...

  2. Python3学习之路~6.3 类变量 VS 实例变量

    类变量 VS 实例变量 #Author:Zheng Na # 实例里面可以查询.增加.删除.修改实例变量 class Role: # 类名 # 类变量 name = '我是类name' n=1 n_l ...

  3. Linux软件包安装

    rpm命令 命名方式  name-VERSION-release.arch.rpm 常见的arch: x86:   i386  i486    i586   i686 x86_64:   x64    ...

  4. Unity3D加密流程文档

    一键搞定源代码.资源等保护 代码被反编译破解,无可奈何? Unity3D 开发的软件程序,无论使用虚拟化,还是混淆,都无法抵抗住黑客丧心病狂的破解,轻松被反编译,令开发商无比头疼. 图片等资源保护被盗 ...

  5. golang 对struct进行Serialize的方法,即将存取二进制文件到struct的方法

    方法一: serialize 的标准方法: 使用gob 和 base64 或 base58. 方法二: 下面是自己实现的 serialize 方法,不推荐自己实现,应该用标准方法. 代码如下: pac ...

  6. Scala数据类型的继承结构

    Scala中,所有的值都是类对象,而所有的类,包括值类型,都最终继承自一个统一的根类型Any.统一类型,是Scala的又一大特点.更特别的是,Scala中还定义了几个底层类(Bottom Class) ...

  7. ngx_lua 模块详细讲解(基于openresty)

    ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...

  8. (转)Fabric 1.0 读写集

    本文译自Fabric 1.0 文档,这篇文档详述了当前读写集语义实现的细节.文档地址为: https://hyperledger-fabric.readthedocs.io/en/latest/rea ...

  9. LineRenderer组建实现激光效果

    在射击游戏中狙击一般都有一个红外线的效果.比如

  10. Oracle之SQL优化专题02-稳固SQL执行计划的方法

    首先构建一个简单的测试用例来实际演示: create table emp as select * from scott.emp; create table dept as select * from ...