DNA Prefix (Light_1224) 动态字典树 + 模板题
来源:互联网 发布:csgo职业选手优化fps 编辑:程序博客网 时间:2024/05/21 17:05
Description
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
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 next n lines contains a non empty string whose length is not greater than 50. And the strings contain characters from {A, C, G, T}.
Output
For each case, print the case number and the maximum result that can be obtained.
Sample Input
3
4
ACGT
ACGTGCGT
ACCGTGC
ACGCCGT
3
CGCGCGCGCGCGCCCCGCCCGCGC
CGCGCGCGCGCGCCCCGCCCGCAC
CGCGCGCGCGCGCCCCGCCCGCTC
2
CGCGCCGCGCGCGCGCGCGC
GGCGCCGCGCGCGCGCGCTC
Sample Output
Case 1: 9
Case 2: 66
Case 3: 20
题目大意:给出一些DNA序列片段,求出这些片段 重复片段数与共同前缀数;
解题思路:动态字典树。在建树的同时更新答案。
代码如下:
#include"iostream"#include"cstring"#include"cstdio"using namespace std;const int maxn = 4;int ans;typedef struct node{node *next[maxn];int cnt;}node,*trie; trie T; int transf(char c){if(c == 'A') return 0;if(c == 'C') return 1;if(c == 'G') return 2;if(c == 'T') return 3;}int max(int x,int y){return x > y ? x : y;}node *newNode(){ //生成一个节点 node *temp = new node;temp -> cnt =0; for(int i = 0;i < maxn;i ++){temp -> next[i] = NULL;}return temp;}void creat(char *str){ //创建字典树 node *p = T;int t,len = strlen(str);for(int i = 0,id = 0;i < len;i ++){id = transf(str[i]);if(p -> next[id] == NULL){p -> next[id] = newNode(); }p = p -> next[id];p -> cnt ++; //在创建同时不断更新ans的值 t = (i + 1) * p -> cnt;ans = max(t,ans);}}void freedom(node *p){for(int i = 0;i < maxn;i ++){if(p -> next[i] != NULL)freedom(p -> next[i]);}delete(p);}int main(){int C,n;char str[55];scanf("%d",&C);for(int k = 1;k <= C;k ++){scanf("%d",&n);T = newNode();ans = 0;while(n --){scanf("%s",str);creat(str);}printf("Case %d: %d\n",k,ans);freedom(T);}return 0;}
- DNA Prefix (Light_1224) 动态字典树 + 模板题
- DNA Prefix--字典树
- DNA Prefix (字典树)
- LightOJ 1224 - DNA Prefix(字典树)
- LightOJ 1224 DNA Prefix(字典树)
- light OJ -DNA Prefix (字典树)
- lightOJ 1244 DNA Prefix (字典树)
- 字典树模板(动态)
- 字典树(需要删除操作)——DNA Prefix ( LOJ 1224 )
- Poj 2001 Shortest Prefix(字典树模板)
- 静态字典树和动态字典树模板
- hdu2846(动态字典树||静态字典树 模板)Repository
- UVa 11488 Hyper Prefix Sets 字典树基础题
- loj 1224 - DNA Prefix
- LightOJ 1224 DNA Prefix
- lightoj1224 DNA Prefix
- 字典树模板题&hdu1251
- UVA11488 字典树模板题
- 使用Centos5在命令行查看DELL R720服务器硬件RAID
- UVa 1025 - A Spy in the Metro
- 零碎笔记(三)
- 通过PreparedStatement 对数据库进行增删改查
- Boost-asio之三
- DNA Prefix (Light_1224) 动态字典树 + 模板题
- 关于app集成DDPushClientDemoUDP消息前的icon不显示问题
- Boost-asio之四
- object-c NSArray排序转载
- Android常见监听事件
- 深入浅出ClassLoader(译)
- 文件操作(NSFileManager)
- Matlab和java混合编程开发
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程