Hyper Prefix Sets

uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2483

题意:给你n个串,对于一个前缀,如果出现k次,就会得到前缀的长度*k,现在让你求长度*k的最大值。

题解:用trie树来搞。把每个串插入到trie中,记录每个子串出现的次数以及长度,trie树很容易实现,然后插完之后,把每个节点遍历一遍,求最大的len*sum,输出即可。

 1 #include<cstring>
 2 #include<vector>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxnode = 4000 * 1000 + 10;
 6 const int sigma_size = 26;
 7 char word[10002];
 8 int n,m;
 9 int sz;
10 struct Trie {
11   int head[maxnode]; // head[i]为第i个结点的左儿子编号
12   int next[maxnode]; // next[i]为第i个结点的右兄弟编号
13   char ch[maxnode];  // ch[i]为第i个结点上的字符
14   int sum[maxnode];
15   int len[maxnode];
16   void clear() {
17     sz = 1;
18     head[0] = next[0] = 0;
19     memset(len,0,sizeof(len));
20     memset(sum,0,sizeof(sum));
21     }
22   void insert(const char *s,int form ,int to) {
23     int u = 0, v;
24     for(int i = form; i <to; i++) {
25       bool found = false;
26       for(v = head[u]; v != 0; v = next[v])
27         if(ch[v] == s[i]) { // 找到了
28           found = true;
29           break;
30         }
31       if(!found) {
32         v = sz++; // 新建结点
33         ch[v] = s[i];
34         next[v] = head[u];
35         head[u] = v; // 插入到链表的首部
36         head[v] = 0;
37       }
38       u = v;
39       sum[u]++;
40       len[u]=i+1;
41     }
42 }
43 }trie;
44 int cas;
45 int main() {
46       scanf("%d",&cas);
47   while(cas--) {
48        trie.clear();
49        scanf("%d",&n);
50        for(int i=1;i<=n;i++){
51         scanf("%s", word);
52            m=strlen(word);
53          trie.insert(word,0,m);
54        }
55        int ans=0;
56        for(int i=1;i<sz;i++){
57           ans=max(ans,trie.len[i]*trie.sum[i]);
58        }
59       printf("%d\n",ans);
60   }
61     return 0;
62 }

Hyper Prefix Sets

时间: 2024-10-07 20:45:05

Hyper Prefix Sets的相关文章

UVA 11488 Hyper Prefix Sets (Trie)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483 Hyper Prefix Sets Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodnes

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

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

uva 11488 - Hyper Prefix Sets(字典树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

UVa 11488 Hyper Prefix Sets

方法:Trie 本题其实就是trie的实现,每个节点需要记录两个值,深度 和 visit的次数,答案便是 max(深度 * visit的次数). 数组实现code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack>

UVA 11488 Hyper Prefix Sets 字典树

模板题,字典树最基本的操作 在看别人的板子的时候学到了一点小技巧 下面贴AC代码,顺便补一补字典树相关,顺便放一下橙子讲课的笔记 Trie三兄弟--标准Trie.压缩Trie.后缀Trie 字符串模式匹配算法--BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 #include<bits/stdc++.h> using namespace std; const int MAX = 5e4 + 5; string s; int n, t, ans; struct Trie {

UVa11488-Hyper Prefix Sets(trie树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

UVA 11488(Hyper Prefix Sets-Trie统计)

Pre x goodness of a set string is length of longest common pre x*number of strings in the set. For example the pre x goodness of the set f000,001,0011g is 6.You are given a set of binary strings. Find the maximum pre x goodness among all possible sub

UVA 11488-Hyper Prefix Sets(Trie)

题意: 给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值 分析: val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树求出最大值 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cst

Trie 字典树

1.UVa 1401 Remember the Word 题意:给出n个字符串集合,问其有多少种组合方式形成目标字符串. 思路:对n个字符串集合建立Trie树,保存每个结点的字符串的顺序编号.然后对这棵树查找目标字符串每一个后缀的前缀字符串,累加. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<vector>