http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F

F - DNA Prefix

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

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)的更多相关文章

  1. LightOJ 1224 - DNA Prefix - [字典树上DFS]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ...

  2. HDU1298 字典树+dfs

    T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  3. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  4. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  5. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  6. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  7. hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs

    题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...

  8. HDU 1298 T9 字典树+DFS

    必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...

  9. hdu 1298 字典树 + DFS (模拟T9文本输入)

    题意:       给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...

随机推荐

  1. Kafka详细的设计和生态系统

    欢迎大家前往云加社区,获取更多腾讯海量技术实践干货哦~ 译者:人工智能资讯小编 本译文自Jean-Paul Azar 在 https://dzone.com 发表的 Kafka Detailed De ...

  2. 使用VMware安装linux虚拟机以及相关配置

    前言 使用VMware安装虚拟机这个一般都知道,操作简单.而本文主要讲使用虚拟机的后续相关配置.并记录使用过程中遇到的问题以及一些技巧.本篇文章以后回持续更新的... 安装包准备 VM:12 Linu ...

  3. 关于mysql的loose index scan的几点疑问

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/102 关于MySQL的loose index scan有几点疑问 ...

  4. cat/tac/more/less 命令详解

    cat:(http://www.cnblogs.com/peida/archive/2012/10/30/2746968.html) *cat主要有三大功能:1.一次显示整个文件:cat filena ...

  5. Java集合(一) CopyOnWriteArrayList

    CopyOnWriteArrayList 类分析   1. CopyOnWriteArrayList 其中底层实现存放数据是一个Object数组:   private volatile transie ...

  6. centos 打包RPM包 ntopng

    需要在centos7上,将ntopng及其依赖的包一起打包成rpm包,了解centos7打包. 1.执行: yum -y install rpmdevtools  安装rpm工具 2.接下来执行:rp ...

  7. Git详解之七:自定义Git

    自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式,介绍了 Git 提供的许多工具来帮助你简单且有效地使用它. 在本章,我将会介绍 Git 的一些重要的配置方法和钩子机制以满足自定 ...

  8. JavaScript的DOM编程--01--js代码的写入位置

    DOM:Document Object Model(文本对象模型) D:文档 – html 文档 或 xml 文档 O:对象 – document 对象的属性和方法 M:模型 DOM 是针对xml(h ...

  9. TurnipBit-MicroPython开发板:跟孩子一起DIY跳动的心

    天是越来越热了,小心脏也是越跳越快啊,为了表达现在激动的心情,必须做个激动的心开始跳动.紧接着就开始带领大家做个激动的心. 首先说说要借助的平台,这次仅仅需要借助一块TurnipBit开发板. Tur ...

  10. DM企业建站系统v201710 sql注入漏洞分析 | 新版v201712依旧存在sql注入

    0x00 前言 本来呢,这套CMS都不想审的了.下载下来打开一看,各种debug注释,排版烂的不行. 贴几个页面看看 感觉像是新手练手的,没有审下去的欲望了. 但想了想,我tm就是新手啊,然后就继续看 ...