LightOJ DNA Prefix(字典树+dfs)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F
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
先给代码吧:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char Str[];
typedef struct Trie{
int val;
Trie *Next[];
}Trie;
Trie root; void creatTrie()
{
int len = strlen(Str);
Trie *p = &root;
Trie *q;
int id;
for(int i = ; i < len; i++){
if(Str[i]=='A') id = ;
else if(Str[i]=='C') id = ;
else if(Str[i]=='G') id = ;
else if(Str[i]=='T') id = ;
if(p->Next[id]==NULL) {
q = new Trie();
q->Next[] = q->Next[] = q->Next[] = q->Next[] = NULL;
q->val=;
p->Next[id] = q;
p = q;
}
else {
q = p->Next[id];
q->val++;
p = q;
}
}
return;
}
int ans;
void sol(Trie *p,int dep)
{
int tm = ;
Trie *q;
for(int i = ; i < ; i++)
{
if(p->Next[i]!=NULL){
q = p->Next[i];
tm = (q->val)*(dep+);
ans = max(tm,ans);
sol(q,dep+);
}
}
}
void clearTree(Trie *t)
{
if(t==NULL) return;
for(int i = ; i < ; i++) {
if(t->Next[i] != NULL) {
clearTree(t->Next[i]);
}
}
delete t;
}
int main()
{
int T;
int cnt = ;
scanf("%d",&T);
while(T--)
{
root.Next[] = root.Next[] = root.Next[] = root.Next[] = NULL;
ans = ;
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%s",Str);
creatTrie();
}
sol(&root,);
printf("Case %d: %d\n",cnt++,ans);
for(int i = ; i < ; i++)clearTree(root.Next[i]);
}
return ;
}
题意:
有n和DNA序列,求出他们中公共前缀长度和有相同公共前缀DNA序列乘积的最大值。
解析:
这题用trie来搞,用trie存储所有的DNA序列,然后每个节点有个val值,还有一个length表示的是存储以该节点结尾的DNA序列的数目,最后求出length*val最大的,就是最终答案。
LightOJ DNA Prefix(字典树+dfs)的更多相关文章
- LightOJ 1224 - DNA Prefix - [字典树上DFS]
题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ...
- HDU1298 字典树+dfs
T9 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs
题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...
- HDU 1298 T9 字典树+DFS
必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...
- hdu 1298 字典树 + DFS (模拟T9文本输入)
题意: 给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...
随机推荐
- Python学习日记:day1
1.计算机基础 cpu:相当于人的大脑,用于计算. 内存:储存数据,运行速度快,成本高,断电数据消失. 硬盘 :固态硬盘(快).机械硬盘(有指针).储存数据,需要长期保持数据,重要文件 打开qq过程: ...
- ftp 只需上传禁止下载
一.首先在ftp / 主目录下给所有用户授予读写权限 二.给子目录授予写入权限,不允许读取
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 三菱Q系列PLC的io分配
1.系统基本配置 2.存储卡配置 3.外部IO标号 4.主基板IO模块的IO号分配 5.扩展基板IO口标号 6.标准配置实例 7. 一.输入采样阶段 在输入采样阶段,可编程逻辑控制器以扫描方式依次地读 ...
- Xamarin使用ListView开启分组视图Cell数据展示bug处理
问题描述 Xamarin使用IsGroupingEnabled="true"之后再Cell操作就会出现数据展示bug,数据不刷新的问题,如下图所示: 点击取消的是其他钢厂,但Vie ...
- ES6常用语法
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
- 树链剖分X2
1.ZJOI树的统计 板子题 因为初始化没打改了几个小时 改到双腿软着出的机房(身体素质感人 #include<iostream> #include<cstdio> #incl ...
- 转载|chrome developer tool—— 断点调试篇
断点,调试器的功能之一,可以让程序中断在需要的地方,从而方便其分析.也可以在一次调试中设置断点,下一次只需让程序自动运行到设置断点位置,便可在上次设置断点的位置中断下来,极大的方便了操作,同时节省了时 ...
- 初学者福音——10个最佳APP开发入门在线学习网站
根据Payscale的调查显示,现在的APP开发人员的年薪达到:$66,851.这也是为什么那么多初学的开发都想跻身到APP开发这行业的主要原因之一.每当你打开App Store时候,看着琳琅满目的A ...
- unity创建和加载AssetBundle
先说一下为什么要使用AssetBundle吧,以前做东西一直忽略这个问题,现在认为这个步骤很重要,代码是次要的,决策和为什么这样搞才是关键. 一句话概括吧,AssetBundle实现了资源与服务分离, ...