题目链接:https://cn.vjudge.net/problem/LightOJ-1224

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

Note
Dataset is huge. Use faster I/O methods.

题意:

给出 $n$ 个字符串(只包含四个字符:$A,C,G,T$),让你从中挑选出 $k$ 个字符串,设他们的最长公共前缀的长度为 $L$。求最大的 $k \cdot L$。

题解:

字典树的每个节点开一个 $pre$ 记录该节点对应的前缀是多少个字符串的前缀。

然后DFS遍历Trie的每个节点,维护节点深度乘节点 $pre$ 值的最大值即可。时间复杂度等于字典树的空间复杂度。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+;
int n;
char s[]; namespace Trie
{
const int SIZE=maxn*;
int sz;
struct TrieNode{
int pre,ed;
int nxt[];
}trie[SIZE];
void init()
{
sz=;
memset(trie,,sizeof(trie));
}
inline int idx(const char& c)
{
if(c=='A') return ;
if(c=='C') return ;
if(c=='G') return ;
if(c=='T') return ;
}
void insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=idx(s[i]);
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
trie[p].pre++;
}
trie[p].ed++;
} int ans;
void dfs(int d,int now)
{
if(!now) return;
ans=max(ans,d*trie[now].pre);
for(int i=;i<;i++) dfs(d+,trie[now].nxt[i]);
}
}; int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%d",&n);
Trie::init();
for(int i=;i<=n;i++)
{
scanf("%s",s);
Trie::insert(s);
}
Trie::ans=;
Trie::dfs(,);
printf("Case %d: %d\n",kase,Trie::ans);
}
}

LightOJ 1224 - DNA Prefix - [字典树上DFS]的更多相关文章

  1. LightOJ DNA Prefix(字典树+dfs)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...

  2. LightOJ 1224 DNA Prefix

    Given a set of n DNA samples, where each sample is a string containing characters from {A, C, G, T}, ...

  3. 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)

    牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...

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

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

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

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

  6. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

  7. 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心

    题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...

  8. BZOJ 1232 [Usaco2008Nov]安慰奶牛cheer:最小生成树【树上dfs性质】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1232 题意: 给你一个无向图,n个点,m条边. 每条边有边权len[i][j],每个点有点 ...

  9. HDU1298 字典树+dfs

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

随机推荐

  1. ASP.NET中TimeSpan的用法

    一.TimeSpan常量.字段 TimeSpan.MaxValue; // 10675199.02:48:05.4775807TimeSpan.MinValue; //-10675199.02:48: ...

  2. [Big Data - ELK] ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台

    ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段: 以下内容来自: http://baidu.blog.51cto.com/71938/1676798 日志主要包括系统日志. ...

  3. 【转】Centos7安装nodejs

    下载及安装步骤: cd /usr/local wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz tar xvf no ...

  4. 关于批判性思维(Critical Thinking)

    批判性思维(CriticalThinking)就是通过一定的标准评价思维,进而改善思维,是合理的.反思性的思维,既是思维技能,也是思维倾向. 批判性思维是在普通思维的基础上又加了第二层思考,并对第一层 ...

  5. 安装 Xshell 5/6 时出现.dll以及0xc000007错误的解决

    安装 Xshell 5/6 时出现.dll以及0xc000007错误的解决 问题:缺少 mfc110.dll或者是其他.dll文件以及应用程序运行错误,如下所示. 方法: 一种是网上直接下载.(缺少. ...

  6. html5——canvas画布

    一.基本介绍 1,canvas是画布,可以描画线条,图片等,现代的浏览器大部分都支持. canvas的width,height默认为300*150,要指定画布大小,不能用css样式的widh,heig ...

  7. compass Errno::EACCES on line ["897"] of C: Permission denied

    具体原因不清楚,应该是与新版的sass有关. 目前的处理方法就是安装原来的版本 gem uninstall compass gem uninstall sass gem install sass –v ...

  8. JavaScript高级用法一之事件响应与网页交互

    综述 本篇的主要内容来自慕课网,事件响应与网页交互,主要内容如下 1 什么是事件 2 鼠标单击事件( onclick ) 3 鼠标经过事件(onmouseover) 4 鼠标移开事件(onmouseo ...

  9. Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization

    I know that blog post title is sure a mouth-full, but it describes the whole problem I was trying to ...

  10. eclipse java ee jsp tomcat Server文件夹给删了,怎么办?

    右键 选择属性 选择Switch location 就可以了