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. KVM 初探

    KVM 是业界最为流行的 Hypervisor,全称是 Kernel-based Virtual Machine.它是作为 Linux kernel 中的一个内核模块而存在,模块名为 kvm.ko,也 ...

  2. ArcGIS 网络分析[2.4] OD成本矩阵

    什么是OD成本矩阵? 先不说这个东西是什么,我们还是举一个实际的例子: 现在存在3个城市北京.上海.武汉,请分析他们两两之间的通行时间. 很简单嘛!北京到上海,北京到武汉,上海到武汉都来一次最短路径分 ...

  3. ArcGIS API for JavaScript 4.2学习笔记[16] 弹窗自定义功能按钮及为要素自定义按钮(第五章完结)

    这节对Popups这一章的最后两个例子进行介绍和解析. 第一个[Popup Actions]介绍了弹窗中如何自定义工具按钮(名为actions),以PopupTemplate+FeatureLayer ...

  4. C#序列化总结

    贴一下自己序列化的代码: public class XMLUtil { /// <summary> /// XML & Datacontract Serialize & D ...

  5. cd 命令详解

    cd 命令 作用:  cd 用来切换目录,目录表示法可为绝对路径或相对路径, 若目录名称省略,则变换至使用者的登陆目录. ~ 可表示为家目录,.为当前目录,..为上级目录 语法: cd (选项)(参数 ...

  6. MVC 框架

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码 ...

  7. Dubbo(二) 认识Zookeeper

    前言 在昨天,我们给大家基本介绍了Dubbo,文中反复提到了Zookeeper,那么它到底是什么呢,这篇文章我们将从Dubbo层面去了解Zookeeper,不做全面讲解,毕竟这是Dubbo教程啊~ Z ...

  8. Linux中gcc编译器的用法

    在Linux环境下进行开发,gcc是非常重要的编译工具,所以学习gcc的基本常见用法时非常有必要的. 一.首先我们先说明下gcc编译源文件的后缀名类型 .c为后缀的文件,C语言源代码文件:  .a为后 ...

  9. 《UNP》学习之TCP状态转换

    CLOSED:TCP起始状态 LISTEN:绑定端口后进入listen状态,一般是服务端 SYN_SENT:发送SYN连接请求,主动打开连接的一方进入SYN_SENT SYN_RCVD:接收到SYN连 ...

  10. iOS 动画篇 (二) CAShapeLayer与CoreAnimation结合使用

    接上一篇博客 iOS 动画篇(一) Core Animation CAShapeLayer是CALayer的一个子类,使用这个类能够很轻易实现曲线的动画. 先来一个折线动画效果: 示例代码: //1. ...