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 ...
随机推荐
- Ubuntu中保存iptables防火墙规则
Ubuntu中保存iptables防火墙规则的例子 打开防火墙 ufw disableufw statusufw enable ufw allow 22/tcp ufw reload iptables ...
- 物联网架构成长之路(23)-Docker练习之Elasticsearch服务搭建
0. 前言 最近基本都是学一些环境配置,和一些中间件的安装与配置.没有实际编写代码.可能看起来有点水,我对自己的学习方式是,先要了解各个中间件的安装配置以及简单使用,理论应用场景,然后我在小项目中,逐 ...
- Linux之sort
sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按AS ...
- 传统D3D11程序面向VS2015编译环境的配置修正细节
A. 配置细节 使用#include <unordered_map>替代<hash_map> 这个是c++标准建议的,没啥好说的 使用#include <directx ...
- 《软件测试自动化之道》读书笔记 之 基于Windows的UI测试
<软件测试自动化之道>读书笔记 之 基于Windows的UI测试 2014-09-25 测试自动化程序的任务待测程序测试程序 启动待测程序 获得待测程序主窗体的句柄 获得有名字控件的 ...
- Spring Security 指定登陆入口
spring security除通过form-login的熟悉指定登陆还可以通过entry-point-ref 指定登陆入口.具体配置如下: <?xml version="1.0&qu ...
- fputc和putc和putchar函数的用法
功 能: 输出一字符到指定流中 putc()与fputc()等价.不同之处为:当putc函数被定义为宏时,它可能多次计算stream的值. 关于fputc(): 原型:int fputc(char c ...
- MYSQL + MHA +keepalive + VIP安装配置(三)--keepalived安装配置
一.概述 keepalived介绍:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除 ...
- Numpy 定义矩阵的方法
import numpy as np #https://www.cnblogs.com/xzcfightingup/p/7598293.html a = np.zeros((2,3),dtype=in ...
- 集群介绍 keepalived介绍 用keepalived配置高可用集群
集群介绍 • 根据功能划分为两大类:高可用和负载均衡 • 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 • 实现高可用的开源软件有:heartb ...