POJ 3080 Blue Jeans (樸素演算法)
阿新 • • 發佈:2020-08-05
題目連結:POJ 3080
Describe: |
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:
|
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的字串,若存在公共子串,則輸出長度最大的子串,若有多個,則輸出字典序小的,如果沒有則按要求輸出一行英文。
解題思路:
暴力。列舉第一行字串的所有子串,判斷該子串是否為其它m-1個字串的子串,如果是,更新ans答案串,並更新最大長度(如果需要的話),詳細註解在註釋給出。
AC程式碼:
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 using namespace std; 5 string a[11],tmp,ans; // a用來存輸入的串,tmp為中間變數,ans為答案串 6 int n,m,flag,len; // n,m為題目輸入變數,flag用來檢查子串是否為其它字串的子串,相當於標誌 7 // len用來存答案串最大長度 8 int main() 9 { 10 cin >> n; 11 while(n--) 12 { 13 ans = ""; // 每一個樣例,先初始化答案串,便於以後判斷 14 len = 0; // 初始化 15 cin >> m; 16 for(int i = 0; i < m; i++) cin >> a[i]; // 輸入 17 // 雙重迴圈,列舉a[0]所有子串,並保證子串長度不小於3(題目要求) 18 for(int j = 0; j <= 57; j++) // 注意j的範圍 19 { 20 for(int i = 3; i + j <= 60; i++) // 注意此處的迴圈條件 21 { 22 flag = 0; // 每個子串判斷前,初始化 23 tmp = a[0].substr(j,i); // 得到子串,切割 24 for(int i = 1; i < m; i++) // 和其他m-1個串做判斷 25 { 26 if(strstr(a[i].c_str(),tmp.c_str()) == NULL) // 判斷是否為子串 27 { 28 flag = 1; 29 break; 30 } 31 } 32 if(flag == 1) continue; // 若不是子串,直接跳過 33 if(ans == "") // 如果第一次得到子串,先賦值 34 { 35 ans = tmp; 36 len = tmp.size(); 37 } else { // 否則,先判斷長度 38 if(tmp.size() > len) 39 { 40 ans = tmp; 41 len = tmp.size(); 42 } else if(tmp.size() == len) { // 若長度相同,則判斷字典序 43 if(strcmp(ans.c_str(),tmp.c_str()) > 0) { 44 ans = tmp; 45 } 46 } 47 } 48 } 49 } 50 // 按要求輸出 51 if(ans == "") cout << "no significant commonalities" << endl; 52 else cout << ans << endl; 53 } 54 return 0; 55 }