Blue Jeans(包含两个查找字符串的重要函数)
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
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT 该题注意除了按字符串长度排序,还有按字符串大小,找出字符串长度最大的来并且字符串最小的 (1)#include<iostream> #include <string> using namespace std; string a[100]; int main() {int t,i,n,j,k; cin>>t; while(t--) {cin>>n; int l=0; string b=""; for(i=0;i<n;i++) cin>>a[i]; for(i=a[0].length();i>=3;i--) { for(j=0;j<a[0].length()-i+1;j++) {bool p=0; string temp = a[0].substr(j, i);//substr()函数返回字符串的一部分。j是开始的地方,i是要提取i个字符串 for(k=1;k<n;k++) if (a[k].find(temp) == string::npos)//// 当 str.find("哦")==string::npos时则说明字符串str中不存在“哦”这个字符, //反之,str.find("哦")!=string::npos则说明字符串str中存在“哦”这个字符{p=1;break;}if(p==0&&(b==""||b>temp))b=temp;}if(b!=""){cout<<b<<endl;l=1;break;}}if(l==0)printf("no significant commonalities\n");}return 0;}/*substr(string,start,length)
string | 必需。规定要返回其中一部分的字符串。//即字符串,当然表示类的函数时候参照上面; |
start | 必需。规定在字符串的何处开始。 正数
- 在字符串的指定位置开始 负数 - 在从字符串结尾的指定位置开始 0 - 在字符串中的第一个字符处开始 |
length | 可选。规定要返回的字符串长度。默认是直到字符串的结尾。 正数 - 从 start 参数所在的位置返回 负数 - 从字符串末端返回 |
echo substr("abcdef", -1); // 输出 "f"
echo substr("abcdef", -2); // 输出"ef"
echo substr("abcdef", -3, 1); // 输出"d"
echo substr("abcdef", 1, -1); //输出 "bcde"
(2)
#include<iostream> #include <string> using namespace std; string a[1000]; int main() {int t,i,n,j,k; cin>>t; while(t--) {cin>>n; string b=""; for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<=a[0].size();i++) { for(j=0;j<=a[0].size();j++) {bool p=0; string temp = a[0].substr(j, i); for(k=1;k<n;k++) {if (a[k].find(temp) == string::npos) {p=1; break;} } if(p==0) if(temp.length()>b.length())b=temp; else if(temp.length()==b.length()&&b>temp)b=temp; } } if(b.size()<3) printf("no significant commonalities\n"); else cout<<b<<endl; } return 0;}