POJ 3080 Blue Jeans (KMP+暴力列舉)【模板】
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
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 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:
Output
- 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.
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 AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTSample Output
no significant commonalities AGATAC CATCATCAT
【題解】
題意:給定長度為60的m個字串,找它的最長公共子串,如果長度相同,輸出字典序小的,如果找到的公共子串小於3 ,就輸出 一串不認識的字母,否則就輸出找到的那一串同樣不認識的字母。
分析:
注意到,這個題給的資料範圍很小,m不大於10,長度不大於60,吐過暴力列舉的話,複雜度大概是60*60*10*(60+60),不會爆,所以果斷列舉,詳細步奏見程式碼註釋;
【AC程式碼】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=65;
int m,n;
char s[11][61]; //記錄輸入的串
char str[61];
char ans[66];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(int i=1;i<=m;++i)
scanf("%s",s[i]);
memset(ans,'\0',sizeof(ans));//注意這裡初始化,不然後面第一次strlen時會出錯
for(int j=1;j<=60;++j)//子串長度
{
int cnt=0; //長度為j的公共串是否找到
for(int k=0;k<=60-j;++k)//遍歷長度為j的子串
{
int flag=1;//標記剩下m-1個串中是否含有長度為j的子串
int w=0;
int ss=k;
while(ss<j+k)
str[w++]=s[1][ss++];
str[w]='\0'; //這裡也注意 後面要用到strlen,所以必須以\0結尾
for(int i=2;i<=m;++i)//遍歷剩下的m-1個串
{
if(!strstr(s[i],str)) //查詢函式 如果str[i]母串中含有str子串返回其下標值 否則返回NULL
{
flag=0;//只要m-1個串中有一個不含有str子串 就直接跳出
break;
}
}
if(flag) //都存在str子串
{
cnt=1; //標記長度為j的公共子串是否存在
if(strlen(str)>strlen(ans)) //長度優先
strcpy(ans,str);
else if(strcmp(str,ans)<0)//字典序小的優先
strcpy(ans,str);
}
}
if(!cnt) break; //注意這裡 很重要的剪枝 如果不存在長為j的公共子串 就更不可能存在長度>J的子串 所以直接跳出
}
if(strlen(ans)<3) //題目要求
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return 0;
}
相關文章
- Blue Jeans 【KMP+暴力】KMP
- 【Kmp】Blue Jeans POJ - 3080KMP
- Poj--3080Blue Jeans+KMP水題KMP
- poj3080-kmp+列舉子串 求最長公共子串KMP
- POJ 1753-Flip Game(列舉&&DFS)GAM
- 暴力列舉- uva11464 - Even Parity
- (UVA - 10976)Fractions Again?!(技巧,暴力列舉)FractionAI
- POJ 1753 Flip Game(列舉變元的高斯消元)GAM
- Codeforces 626D Jerry's Protest(暴力列舉+概率)
- (桶暴力列舉gcd) CF1627D Not AddingGC
- POJ2774Long Long Message(字尾陣列模板)陣列
- UVALive 7410 && POJ 5583 Kingdom of Black and White (列舉)
- 6343 密碼鎖 CSP-S 2023年 暴力列舉密碼
- 5211 導彈攔截 普及組 NOIP2010 暴力列舉
- Java 列舉、JPA 和 PostgreSQL 列舉JavaSQL
- 列舉和列舉的取值範圍
- POJ 2718簡單列舉貪心演算法(好久沒寫程式碼了。。)演算法
- vue面試題:列舉說明vue的模板語法和常用指令?Vue面試題
- Java列舉Java
- Swift,列舉Swift
- KMP+狀態轉移KMP
- poj 2481 樹狀陣列陣列
- POJ 1305-Fermat vs. Pythagoras(本原的畢達哥拉斯三元組+列舉)Go
- C# 列舉與位列舉概述C#
- 列舉工具類
- TypeScript 列舉enumTypeScript
- Java 列舉(enum)Java
- Swift-列舉Swift
- 自定義列舉
- java列舉類Java
- TypeScript 列舉指南TypeScript
- Java列舉使用Java
- C#:列舉C#
- 列舉程式 (轉)
- 列舉比較
- 列舉型別型別
- CI/CD流程 命令列方式與Jenkins Blue Ocean方式命令列Jenkins
- Java列舉-通過值查詢對應的列舉Java