DNA Prefix (字典树)
来源:互联网 发布:jquery ui.min.js下载 编辑:程序博客网 时间:2024/05/21 09:12
Given a set of n DNA samples, where each sample is a string containing characters from{A, C, G, T}, we are trying to find a subset of samples in the set, where the length of the longest common prefix multiplied by the number of samples in that subset is maximum.
To be specific, let the samples be:
ACGT
ACGTGCGT
ACCGTGC
ACGCCGT
If we take the subset {ACGT} then the result is 4 (4 * 1), if we take {ACGT, ACGTGCGT, ACGCCGT} then the result is 3 * 3 = 9 (since ACG is the common prefix), if we take {ACGT, ACGTGCGT, ACCGTGC, ACGCCGT} then the result is 2 * 4 = 8.
Now your task is to report the maximum result we can get from the samples.
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 50000) denoting the number of DNA samples. Each of the nextn lines contains a non empty string whose length is not greater than50. And the strings contain characters from {A, C, G, T}.
For each case, print the case number and the maximum result that can be obtained.
3
4
ACGT
ACGTGCGT
ACCGTGC
ACGCCGT
3
CGCGCGCGCGCGCCCCGCCCGCGC
CGCGCGCGCGCGCCCCGCCCGCAC
CGCGCGCGCGCGCCCCGCCCGCTC
2
CGCGCCGCGCGCGCGCGCGC
GGCGCCGCGCGCGCGCGCTC
Case 1: 9
Case 2: 66
Case 3: 20
题意:给出几串DNA序列(只包含“ACGT”),求最长的前缀和(相同前缀所包含的字符个数乘字符串个数);
通过这个题引入了trie树(字典树)的概念;(以下来自百度百科)
1.定义:又称单词查找树,Tire树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
2.性质:
(1)根节点不包含字符,除根节点外每一个节点都只包含一个字符;
(2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串;
(3)每个节点的所有子节点包含的字符都不相同;
(1)串的快速检索
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <iostream>#define MAX 4using namespace std;struct Trie{ Trie *next[MAX]; int cnt;}*root;int ans;/****new一个新指针,开辟空间****/Trie *newTrie(){ Trie *temp = new Trie; for(int i=0; i<MAX; i++) temp->next[i]=NULL; temp->cnt=0; return temp;}/****释放空间,避免超内存****/void freedom(Trie *p){ for(int i=1; i<MAX; i++){ if(p->next[i]!=NULL) freedom(p->next[i]); } delete(p);}/****建立trie树****/void SetTrie(string s){ Trie *p=root; int t; int len=s.size(); int id; for(int i=0; i<len; i++){ switch(s[i]){ case 'A': id=0; break; case 'C': id=1; break; case 'G': id=2; break; case 'T': id=3; break; } if(p->next[id]==NULL){ p->next[id]=newTrie(); } p=p->next[id]; p->cnt++; t=(i+1)*p->cnt; ans=max(ans, t); }}int main(){ int T; cin >> T; int Case=0; while(T--){ Case++; root=newTrie(); int n; cin >> n; string s; ans=0; while(n--){ cin >> s; SetTrie(s); } printf("Case %d: %d\n", Case, ans); freedom(root); } return 0;}
- DNA Prefix (字典树)
- DNA Prefix--字典树
- LightOJ 1224 DNA Prefix(字典树)
- light OJ -DNA Prefix (字典树)
- lightOJ 1244 DNA Prefix (字典树)
- LightOJ 1224 - DNA Prefix(字典树)
- 字典树(需要删除操作)——DNA Prefix ( LOJ 1224 )
- DNA Prefix (Light_1224) 动态字典树 + 模板题
- HDU 5790 Prefix(字典树、主席树)
- Poj 2001 Shortest Prefix(字典树模板)
- uva 11488 - Hyper Prefix Sets(字典树)
- UVa 11488 Hyper Prefix Sets(字典树)
- hihoCoder 1107 Shortest Proper Prefix (字典树的遍历)
- UVA 11488 Hyper Prefix Sets(字典树)
- loj 1224 - DNA Prefix
- LightOJ 1224 DNA Prefix
- lightoj1224 DNA Prefix
- hdu 5790 Prefix(字典树+主席树)
- UVA11178 计算几何入门题
- Ubuntu安装、基本命令和常见故障处理
- iOS开发造轮子 | UIView及其子类的占位图
- iOS开发造轮子 | 优雅的封装一个倒计时button
- nyoj111分数加减法
- DNA Prefix (字典树)
- thread中join和detach的区别
- 异常控制流
- 安卓开发-Broadcast接受者+六种常见Broadcast接受者案例+进程的优先级
- 轻松告别OpenCV Manager
- 图解什么是编译程序?程序设计语言典型的处理过程(预处理、编译、汇编)
- Python的生成器表达式与生成器函数
- 求解字符串的最长回文子串的Manacher’s Algorithm
- c++经典题---巧用算法输出A组成的三角形