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. slowhttptest慢攻击工具介绍

    slowhttptest介绍 Slowhttptest是依赖HTTP协议的慢速攻击DoS攻击工具,设计的基本原理是服务器在请求完全接收后才会进行处理,如果客户端的发送速度缓慢或者发送不完整,服务端为其 ...

  2. 数据库入门(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除.和修改.可以说,数据库是长期存储在计算 ...

  3. 关于git的一些理论知识

    一.什么是版本控制器 好多刚用git的coder一说起git,就随口会说出版本控制器嘛,我问那是干嘛的,大部分人就回答上传代码的.然后会用,但是有些理论你问他们他们就不知道了,比如不是代码的文件就不能 ...

  4. bat修改密码

    @echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...

  5. 保存html上传文件过程中遇到的字节流和字符流问题总结

    java字节流和字符流的区别以及相同 1. 字节流文件本身进行操作,字符流是通过缓存进行操作, 1.1 使用字节流不执行关闭操作 File f =new File("d:/test/test ...

  6. 笔记-CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用情况

    //CGRectInset 将原来的矩形放大或者缩小,正表示缩小,-表示放大. CGRect rect= CGRectMake(20, 50, 100, 80); CGRect rect1=CGRec ...

  7. 七行代码开始flask

    前言: 对于现有的企业接口服务实现方式来说,Java比较适用于大型的并发式的业务场景:而对一些低IO的且功能简单的数据接口来说,Python似乎更合适.近几年流行的Flask可以说是专为接口式开发而生 ...

  8. webapi 异步返回

    两年前我遇到一个难题: https://q.cnblogs.com/q/78177 WebAPI中使用socket如果在server端回复了再返回值? 现在终于做出一种实现了: [HttpGet]   ...

  9. C#调用windows api 实现打印机控制

    using System; using System.Text; using System.Runtime.InteropServices; using System.Security; using ...

  10. python3基础(七)函数基础

    Function 函数是一段组织好的能够实现特定功能或者逻辑的代码块,函数代码在文件执行时读入内存并不执行,在调用函数时执行,简单来说是把一段代码封装给一个函数名(可以用变量的概念去理解,即把一段代码 ...