Description
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
- 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
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
自己开始错误代码(原因:简单的找前一部分的最长子列,但是前面最长的不能代表后面全部的公共之列也是最长的,例如
3 abcdefpppfdsa abcdefkkkfdsa abckkofdsa)
错误代码:
#include<iostream> #include <string> #include<algorithm> using namespace std; int main( ) { int i,j,x,y,l=3,u; char str[10][65],max[65],strin[65],String[65]; max[0]='\0'; int n; cin>>n; for(i=0;i<n;i++) cin>>str[i]; strcpy(String,str[0]); for(u=1;u<n;u++) { for(x=0;x<strlen(String);x++)//////// { i=x; for(y=0;y<strlen(str[u]);y++) { j=y; l=0; i=x; for(;String[i]==str[u][j]&&i<strlen(String)&&j<strlen(str[u]);i++,j++) //////// //for(i++,j++;str1[i]==str2[j];i++,j++);这样和for(i++,j++;str1[i]==str2[j];i++,j++){}用法相同 { strin[l]=String[i]; l++; } strin[l]='\0'; if(strlen(max)<strlen(strin)) strcpy(max,strin);
//max[strlen(max)]='\0'; } } strcpy(String,max); max[0]='\0'; cout<<String<<endl; cout<<max<<endl; } if(strlen(max)<3) cout<<"no significant commonalities"<<endl; else cout<<max<<endl; return 0;
正确代码:
本题中引用两个库函数strncpy(s,p[0]+i,j-i+1)字符串复制函数和strstr(p[k],s)字符串查找函数 复制到s数组 被复制起始地址p[0]+i 复制长度 要查找的大数组 被查找的子数列
正确代码:
#include<cstdio> #include<cstring>
int main(void) { int loop; scanf("%d",&loop); while(loop--) { int m; int i; char p[11][65]; scanf("%d",&m); for(i=0;i<m;i++) scanf("%s",p[i]); int len; char ans[65]; len=0; for(i=0;i<strlen(p[0]);i++) ///////////////////////要查找串的头部 for(int j=i+2;j<strlen(p[0]);j++) ///////////////////要查找的串的尾部 { char s[65]; strncpy(s,p[0]+i,j-i+1);//字符串复制函数 s[j-i+1]='\0'; bool ok=true; for(int k=1;ok&&k<m;k++) ////////在串中从第一个开始查找当前所选的串 if(strstr(p[k],s)==NULL)//字符串查找函数 { ok=false; break;////////////如果出现匹配失败时直接跳出 } if(ok==false) break; /////////////////////如果出现匹配失败的情况直接跳出当前串的查找,因为已经出现不能匹配的串了 if(ok&&(j-i+1>=len&&strcmp(ans,s)>0))//////////////////该处未理解?????????????? { len=j-i+1; strcpy(ans,s); } } if(len<3) printf("%s\n","no significant commonalities"); else { printf("%s\n",ans); } } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现