[poj 3080]Blue Jeans [kmp]
来源:互联网 发布:深圳创意软件 编辑:程序博客网 时间:2024/05/22 15:20
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.
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
32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalitiesAGATACCATCATCAT
Source
South Central USA 2006
思路很简单:我们先按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以
思路很简单:我们先按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以
#include<iostream>#include<cstring>#include<stdio.h>using namespace std;const int Max = 62;char text[10][Max], pat[Max];int n, ma, lenp, next[Max];void get_next(){ int i = 0, j = -1; next[0] = -1; while(i < lenp) { if(j == -1 || pat[i] == pat[j]) { i ++; j ++; next[i] = j; } else j = next[j]; }}void KMP(){ int k, m, i, j; get_next(); ma = 100; for(k = 1; k < n; k ++) { i = 0; j = 0; m = 0; while(i < 60 && j < lenp) { if(j == -1 || text[k][i] == pat[j]) { i ++; j ++; } else j = next[j]; if(j > m) m = j; } if(m < ma) ma = m; }}int main(){ int t, i; char result[Max]; scanf("%d", &t); while(t --) { scanf("%d", &n); for(i = 0; i < n; i ++) scanf("%s", text[i]); int ans = 0; for(i = 0; i <= 57; i ++) { strcpy(pat, text[0] + i); // 枚举第一个串的所有后缀串(当然最后2个可以省去)。 //cout<<pat<<endl; lenp = 60 - i; KMP(); // KMP求出这个后缀串与其余所有串的最大匹配。 if(ans < ma) { ans = ma; strncpy(result, text[0] + i, ans); result[ans] = '\0'; } else if(ans == ma) { // 存在多个最长公共子串,输出字典序最小的,WA了一次。 char tmp[Max]; strncpy(tmp, text[0] + i, ans); // 复习: strncpy()没有复制最后的'\0'。 tmp[ans] = '\0'; if(strcmp(tmp, result) < 0) strcpy(result, tmp); } } if(ans >= 3) printf("%s\n", result); else printf("no significant commonalities\n"); } return 0;}
1 0
- POJ 3080 Blue Jeans KMP
- POJ 3080 Blue Jeans----kmp
- POJ 3080 Blue Jeans(KMP)
- POJ 3080 Blue Jeans KMP
- poj 3080 Blue Jeans(KMP)
- poj-3080 Blue Jeans(KMP)
- [poj 3080]Blue Jeans [kmp]
- POJ 3080 Blue Jeans KMP
- 【Kmp】Blue Jeans POJ
- poj 3080 Blue Jeans kmp+枚举
- [KMP或者暴力]POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans 暴力枚举+KMP
- POJ 3080 Blue Jeans (KMP)
- poj 3080 Blue Jeans ( 二分 + kmp)
- POJ 3080 Blue Jeans KMP+暴力
- poj 3080 Blue Jeans(KMP)
- POJ 3080 Blue Jeans KMP解法
- poj 3080 Blue Jeans(数据结构:KMP)
- hdu 3979 Monster
- 关于execv函数
- 并查集三
- iOS开发——创建依赖工程Framework
- C++中成员变量的初始化总结
- [poj 3080]Blue Jeans [kmp]
- Android轮播图实现
- Spark 2.0介绍:Spark SQL中的Time Window使用
- 排序算法总结
- 有向图最小树形图
- 二级指针的使用
- 用变量a给出下面的定义:一个有10个指针的数组,该指针指向一个函数,该函数有一个整形参数并返回一个整型数
- 海量数据处理面试题
- 排序算法------快速排序