POJ 3080 字符串水题
2015-11-24 00:41
423 查看
Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15076 Accepted: 6691
Description
The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.
As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers.
A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC.
Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
m lines each containing a single base sequence consisting of 60 bases.
Output
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string “no significant commonalities” instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
题意:
求几个串的公共连续字串,如果这个公共串长度小于三就不算。如果有很多个最长长度的串。那么输出长度最短的一个。
题解:
拿到题看到60单位的字符串还是有点吃惊。想是不是KMP啊。 。trie树啊。 。dp啊什么。 。后来发现直接暴力就好。 。虽然是有O(n*k^3)的复杂度。 。但是k = 60,n = 10总共才200w+,比计算机的运算速度还是小。直接枚举子集就可以了。这里有两个地方值得注意:
一是strncpy是可拿来吧s2的一段长度复制给s1。但是这个操作是不会带上’\0’的。这点与strcpy不一样。
二是求字典序可以直接用C++里string类来,s1
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15076 Accepted: 6691
Description
The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.
As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers.
A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC.
Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
m lines each containing a single base sequence consisting of 60 bases.
Output
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string “no significant commonalities” instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
题意:
求几个串的公共连续字串,如果这个公共串长度小于三就不算。如果有很多个最长长度的串。那么输出长度最短的一个。
题解:
拿到题看到60单位的字符串还是有点吃惊。想是不是KMP啊。 。trie树啊。 。dp啊什么。 。后来发现直接暴力就好。 。虽然是有O(n*k^3)的复杂度。 。但是k = 60,n = 10总共才200w+,比计算机的运算速度还是小。直接枚举子集就可以了。这里有两个地方值得注意:
一是strncpy是可拿来吧s2的一段长度复制给s1。但是这个操作是不会带上’\0’的。这点与strcpy不一样。
二是求字典序可以直接用C++里string类来,s1
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <string> #define f(i,a,b) for(int i = a;i<=b;i++) #define fi(i,a,b) for(int i = a;i>=b;i--) using namespace std; char G[15][65]; char tem[65]; int main() { // freopen("data.in","r",stdin); int N; scanf("%d",&N); while(N--){ int n; scanf("%d",&n); string s1 = ""; int Max = -0x3f3f3f3f; f(i,1,n) scanf("%s",G[i]); int len = strlen(G[1]); int ok = 0; f(i,0,len-1) f(j,3,len-i){ // memset(tem,0,sizeof(tem)); strncpy(tem,G[1]+i,j); tem[j] = '\0'; int k; for(k = 2;k<=n;k++) if(!strstr(G[k],tem)) break; if(k<=n); else{ ok = 1; if(j>Max){ Max = j; s1 = tem; }else if(j == Max){ string s2 = tem; if(s2<s1) s1 = s2; } } } if(ok) cout << s1 << endl; else printf("no significant commonalities\n"); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板