UVa 11148 Hyper Prefix Sets (Trie)
来源:互联网 发布:美国传播学专业知乎 编辑:程序博客网 时间:2024/05/22 14:54
题目链接:https://vjudge.net/problem/UVA-11488
题意:给定n个字符串,从n个字符串中选出若干个组成字符串集合S。定义P(S)为集合S中所有串的最长公共前缀长度与S中字符串个数的乘积。求一个集合S,使得P(S)最大。
思路:首先将所有串建成一颗Trie树,然后遍历整个Trie树。当遍历到某个结点u时,从起始根节点往下到u构成了一个前缀,以这个为前缀的字符串个数即为u及其子树中单词结点的个数,这两个的乘积便是一个答案。边遍历边统计答案即可。
#include<cstdio>#include<cstring>#include<string>#include<cctype>#include<iostream>#include<set>#include<map>#include<cmath>#include<sstream>#include<vector>#include<stack>#include<queue>#include<algorithm>#define fin freopen("a.txt","r",stdin)#define fout freopen("a.txt","w",stdout)typedef long long LL;typedef unsigned long long ULL;using namespace std;const int inf = 1e9 + 10;const int maxnode = 1e7 + 10;const int sigma_size = 2;const int maxn = 35;LL res;struct Tree{int ch[maxnode][sigma_size];int val[maxnode];int depth[maxnode], size[maxnode]; int sz; int idx(char c) { return c - '0'; } void init() { memset(ch[0], 0, sizeof ch[0]); sz = 1; } void insert(char *s) { int n = strlen(s), u = 0; for(int i = 0; i < n; i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz], 0, sizeof ch[sz]); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; depth[u] = i+1; } val[u]++; } int Find(int u) { int ans = 0; for(int i = 0; i < 2; i++) { int c = ch[u][i]; if(c) ans += Find(c); } if(val[u]) ans += val[u]; res = max(res, (LL)depth[u] * ans); return size[u] = ans; }}tree;int main(){int T, n;char s[205];scanf("%d", &T);while(T--){tree.init();scanf("%d", &n);for(int i = 1; i <= n; i++){scanf("%s", s);tree.insert(s);}res = 0;tree.Find(0); cout << res << endl;}return 0;}
阅读全文
0 0
- UVa 11148 Hyper Prefix Sets (Trie)
- uva 11488 - Hyper Prefix Sets(Trie)
- UVa 11488 - Hyper Prefix Sets (Trie)
- UVA 11488 Hyper Prefix Sets(trie的应用)
- UVA 11488-Hyper Prefix Sets(trie树+DFS)
- UVA 11488 Hyper Prefix Sets(Trie)
- UVA 11488,Hyper Prefix Sets,Trie树
- UVA 11488 - Hyper Prefix Sets(Trie)
- Hyper Prefix Sets - UVa 11488 Trie树
- UVA 11488 Hyper Prefix Sets (Trie)
- UVA - 11488 - Hyper Prefix Sets(Trie 应用)
- UVA 11488 Hyper Prefix Sets Trie树求最值
- UVA 11488(Hyper Prefix Sets-Trie统计)
- UVa11488-Hyper Prefix Sets(trie树)
- uva11488 Hyper Prefix Sets(Trie树)
- Uva-11488-Hyper Prefix Sets
- UVa:11488 Hyper Prefix Sets
- UVA - 11488 Hyper Prefix Sets
- HDU 6161 Big binary tree(树形DP)
- tensorflow学习笔记(六):cnn过程可视化
- iOS关于模块化开发解决方案(纯干货)
- 常考排序算法
- android ril 深度学习
- UVa 11148 Hyper Prefix Sets (Trie)
- 毕业以后上百万大学生去哪了
- 计算机学习网站汇总
- 如何批量修改Word文档中的表格样式和题注格式?
- 富文本编辑器react-quill的使用
- Java 多线程基础
- Android中的动画2(逐帧动画)
- 你人生中的那口井挖了没有?
- Java安全学习笔记(四)--CBC方式解密