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 ...
随机推荐
- JAVA Socket编程(一)之UDP通信
常见的通讯协议有udp和tcp. --将数据及源.目的封装在数据包中,不需要建立连接: --每个数据包的大小限制在64k以内: --因无连接,是不可靠协议: --不需要建立连接,所以传输速度快,但是容 ...
- ubuntu16.04 查询ip,网关,dns信息
用ifconfig命令只能查询ip,子网掩码信息,不能获取dns和网关信息 用下面命令即可查询 nmcli dev show
- C#设计模式之二十二备忘录模式(Memeto Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就 ...
- 【WebGL】《WebGL编程指南》读书笔记——第6章
一.前言 最近重感冒发烧,妈蛋好难受,请假了3天,驾校也没去,简直僵硬!今天继续WebGL的学习. 二.正文 A. GLSL支持两种数据值类型: 整数型(int)与浮点型( ...
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- hashCode花式卖萌
声明:这篇博文纯属是最近看源码时闲着没事瞎折腾(好奇心驱动),对实际的应用程序编码我觉得可能没有那么大的帮助,各位亲就当是代码写累了放松放松心情,视为偏门小故事看一看就可以了,别深究. 一.从Obje ...
- C# DataGridVie利用model特性动态加载列
今天闲来无事看到ORm的特性映射sql语句.我就想到datagridview也可以用这个来动态添加列.这样就不用每次都去界面上点开界面填列了. 代码简漏希望有人看到了能指点一二. 先定义好Datagr ...
- 任务调度 -----> quartz 不同时间间隔调度任务
Quartz Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运 ...
- 如何用Visio画venn(维恩)图
今天需要换几个Venn(维恩)图,按照以前的套路是用画图工具来画的,但是这次不是画给自己看,并且也要很迅速的画好,那就迅速的来学习了. 参考网址:https://support.office.com/ ...
- python的time模块常用内置函数
1.Python time time()方法 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). time()方法语法: time.time() 举例: #! ...