LightOJ 1224 DNA Prefix
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.
字典树,保证空间充足否则re。
代码:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
using namespace std;
int n,t,pos;
int trie[][];
int num[];
int ans;
int to[];
void Insert(char *s) {
int i = ,c = ;
while(s[i]) {
int d = to[s[i]];
if(!trie[c][d]) {
trie[c][d] = ++ pos;
}
c = trie[c][d];
ans = max(ans,++ num[c] * ++ i);
}
}
int main() {
to['A'] = ;
to['C'] = ;
to['G'] = ;
to['T'] = ;
scanf("%d",&t);
char s[];
for(int i = ;i <= t;i ++) {
scanf("%d",&n);
pos = ;
memset(num,,sizeof(num));
memset(trie,,sizeof(trie));
ans = ;
for(int j = ;j < n;j ++) {
scanf("%s",s);
Insert(s);
}
printf("Case %d: %d\n",i,ans);
}
}
LightOJ 1224 DNA Prefix的更多相关文章
- LightOJ 1224 - DNA Prefix - [字典树上DFS]
题目链接:https://cn.vjudge.net/problem/LightOJ-1224 Given a set of $n$ DNA samples, where each sample is ...
- 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
很简单的Trie树,记录每个前缀的次数,dfs一次Trie即可 #include<set> #include<map> #include<list> #includ ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- 【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper- ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- prefix sums--codility
lesson 5: prefix sums 1. PassingCars 2. CountDiv 3. GenomicRangeQuery 4. MinAvgTwoSlice lesson 5: pr ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
- hbase_异常_03_java.io.EOFException: Premature EOF: no length prefix available
一.异常现象 更改了hadoop的配置文件:core-site.xml 和 mapred-site.xml 之后,重启hadoop 和 hbase 之后,发现hbase日志中抛出了如下异常: ...
随机推荐
- OpenGL学习进程(10)第七课:四边形绘制与动画基础
本节是OpenGL学习的第七个课时,下面以四边形为例介绍绘制OpenGL动画的相关知识: (1)绘制几种不同的四边形: 1)四边形(GL_QUADS) OpenGL的GL_QUADS图 ...
- Plist文件与数据解析
综述 初步阶段当我们做个需要点数据的练习时(比如购物商品展示),我们可能是将数据直接写在代码中,比如说定义一个字符串数组或存放字典的数组.但这其实并不是一种合理的做法.因为如果当数据修改的时候,就要经 ...
- 斯坦福机器学习视频笔记 Week7 支持向量机 Support Vector Machines
SVM被许多人认为是最强大的“黑箱”学习算法,并通过提出一个巧妙选择的优化目标,今天最广泛使用的学习算法之一. Optimization Objective 根据Logistic Regression ...
- Eclipse与Tomcat的集成(无插件)
1.下载Eclipse(https://www.eclipse.org/downloads/)和Tomcat(http://tomcat.apache.org/),具体的安装略: 2.打开Eclips ...
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- struts2 if标签示例[转]
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
- 自学Hadoop
一.Hadoop基础设施 起源于Google的三篇论文: 1. <The Google File System > 2003年 http://static.googleuserconten ...
- Java之File文件类
package IoDemo; import java.io.File; import java.io.FileFilter; import java.io.IOException; import j ...
- static 关键字详解 static方法调用非static属性和方法
静态的属性和方法在内存中的存放地址与非静态的是不同的,静态的是存放在static区,它意味着静态方法是没有this的,所以我们不可以从一个static方法内部发出对非static方法的调用.但是反之是 ...
- network namespace连接的4种方法及性能
veth pair # add the namespaces ip netns add ns1 ip netns add ns2 # create the veth pair ip link add ...