LightOJ 1224 - DNA Prefix - [字典树上DFS]
题目链接: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]的更多相关文章
- LightOJ DNA Prefix(字典树+dfs)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...
- LightOJ 1224 DNA Prefix
Given a set of n DNA samples, where each sample is a string containing characters from {A, C, G, T}, ...
- 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)
牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs
题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...
- 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 ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- BZOJ 1232 [Usaco2008Nov]安慰奶牛cheer:最小生成树【树上dfs性质】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1232 题意: 给你一个无向图,n个点,m条边. 每条边有边权len[i][j],每个点有点 ...
- HDU1298 字典树+dfs
T9 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
随机推荐
- 【转】CLR和JIT的理解、.NET反汇编学习
CLR:通用语言运行时(Common Language Runtime)的简称,CLR是.NET框架的核心内容之一,可以把它看为一套标准资源,可以呗任何.NET程序使用.它包括:面向对象的编程模型.安 ...
- SQL Server死锁产生原因及解决办法 .
其实所有的死锁最深层的原因就是一个:资源竞争 表现一: 一个用户A 访问表A(锁住了表A),然后又访问表B,另一个用户B 访问表B(锁住了表B),然后企图访问表A,这时用户A由于用户B已经锁住表B,它 ...
- arcgis server缓存路径修改
由于空间不够用,需要更换瓦片的输出路径,具体的修改方法如下: 1.打开ArcCatalog,打开GIS服务器,找到已经添加的gis服务器,一般都是机器名,如下所示,右键我的gis服务器(admin-t ...
- angularjs入门初体验
1. http://www.zouyesheng.com/angular.html#toc39
- Cubieboard A10 安装Nand系统,配置nginx,php,mysql,samba详细教程
安装前置条件 1.下载win32diskimager-v0.7-binary.zip 2.下载debian_wheezy_armhf_v1_mele.zip 3.下载cubie_nand_uboot_ ...
- maven安装及与eclipse集成
一.maven安装 1,下载地址:http://maven.apache.org/download.cgi 2,解压maven到:C:\Java\apache-maven-3.5.3,目录可以随意. ...
- 红米3 MoKee 7.1.2_r36 自编译版/去魔趣中心、宙斯盾/息屏禁止刷新UI 2018年5月5日更新
一.ROM简介 MoKee是基于CM二次修改的ROM,本地化系统:农历.归属地.OMS框架.状态栏显示网速/时间显秒等等. 二.ROM自编译DIY简介 1.Lawnchair桌面. 2.Via谷歌版浏 ...
- psd页面切割成html技巧总结
关键字:psd切割技巧 生成html图片要点 css样式要点 rem 与高手切割后的代码对比学习提高(考察点:切割后的页面质量,源码大小及图片大小,js技术,动画技术,开发和命名规范等) 一.psd切 ...
- linux下Ftp服务安装
安装vsftp 使用yum命令安装vsftp #yum install vsftpd -y 如果yum安装不成功,可以到 http://pkgs.org/centos-6/centos-x86_64/ ...
- CentOS7 yum方式安装MariaDB 10.2.13-1
注:以下步骤都是以root身份运行. 一.建立mariadb.repo 1,编辑新文件,命令:vim /etc/yum.repos.d/mariadb.repo 2,输入如下内容,保存退出 [mar ...