Blue Jeans 【KMP+暴力】
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.
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
題目大意:給出n串字串,求每一串都含有的最長子序列,並且如果有多個答案,輸出字母序靠前的那個;
思路:根據題意可知,這道題資料範圍很小,每個串長度為六十,所以直接暴力,列舉第一串的所有子串,然後判斷在後面的串中是否都出現過;
上程式碼:
由於資料比較水,這個程式碼有點錯誤也AC了;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=105;
int Next[15][N];
char s[15][N];
char tmp[N];//臨時串
char ans[N];//答案串
void get_Next(int m)
{
int i=0,j=-1;
Next[m][0]=-1;
while(i<60)
{
if(j==-1||s[m][i]==s[m][j])
Next[m][++i]=++j;
else
j=Next[m][j];
}
}
int kmp(int n,int m)//KMP核心程式碼,如果能找到返回true;
{
int i=0,j=0;
while(i<60&&j<n)
{
if(j==-1||tmp[j]==s[m][i])
{
i++;
j++;
}
else
j=Next[m][j];
}
if(j>=n)
return true;
else
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(Next,0,sizeof(Next));
memset(ans,'\0',sizeof(ans));
int i,n,j,t,l,k,flag=0,vis1,vis2,len;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%s",s[i]);
get_Next(i);//直接得到每一串的Next陣列,方便使用
}
for(i=59; i>=2; i--)//代表每次列舉當前串的長度
{
vis2=1;
for(j=0; j+i<60; j++)
{
vis1=0;
memset(tmp,'\0',sizeof(tmp));
for(k=0; k<=i; k++)//將字串新增進臨時串
tmp[k]=s[0][j+k];
for(k=1; k<n; k++)
{
if(!kmp(i+1,k))
{
vis1=1;
break;
}
}
if(vis1==0)
{
vis2=0;
if(tmp[0]<ans[0]||ans[0]=='\0')//如果ans串為空或者字母序更大,複製臨時串刀ans串
strcpy(ans,tmp);
}
}
if(vis2==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return 0;
}
這個是正確程式碼
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
char s[15][75];
int nex[75];
char c[75];
void get_nex(char s[])
{
int k=-1;
int j=0;
nex[0]=-1;
while(j<60)
{
if(k==-1||s[j]==s[k])
{
j++;
k++;
nex[j]=k;
}
else
k=nex[k];
}
}
bool KMP(char s[],char t[])
{
int i=0,j=0;
int len1=strlen(s);
int len2=strlen(t);
while(i<len1&&j<len2)
{
if(j==-1||s[i]==t[j])
{
i++;
j++;
}
else
j=nex[j];
}
if(j==len2)
return true;
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int now=1;
int tmp=60;
for(int i=1; i<=n; i++)
scanf("%s",s[i]);
char ans[75]="";
int flag=0;
for(int len=tmp; len>=3; len--)
{
for(int i=0; i<=tmp-len; i++)
{
strncpy(c,s[now]+i,len);
c[len]='\0';
get_nex(c);
int j=1;
for(j=1; j<=n; j++)
{
if(j==now) continue;
if(!KMP(s[j],c))
{
break;
}
}
if(j==n+1)
{
flag=1;
if(strcmp(c,ans)<0||!strlen(ans))
strncpy(ans,c,strlen(c));
}
}
if(flag) break;
}
if(flag==0)
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return 0;
}
相關文章
- Poj--3080Blue Jeans+KMP水題KMP
- KMP+狀態轉移KMP
- Blue Cat Audio Blue Cat PatchWork mac(藍貓橋接外掛)Mac橋接
- Practical Estimators (MLE, BLUE, LS)
- 多頻段調音臺:Blue Cat Audio Blue Cats MB-7 Mixer for macMac
- [Blue Prism] Data item 的使用
- 暴力破解
- [AGC064D] Red and Blue Chips 題解GC
- win10啟動blue stacks很慢怎麼辦_win10啟動blue stacks很慢的修復方法Win10
- C++暴力指南C++
- 什麼是暴力破解?暴力破解的方法有哪些?
- 南洋理工:研究發現暴力遊戲與暴力行為無關遊戲
- [暴力 Trick] 根號分治
- Adjustment Office 暴力模擬
- 從暴力中解脫,
- 暴力破解測試
- 真實的描寫暴力,讓我們真實地痛恨著暴力
- Grace Blue:2019亞太區人才流動報告
- CI/CD流程 命令列方式與Jenkins Blue Ocean方式命令列Jenkins
- 快手嚴打網路暴力:整治網路暴力不能一刀切
- 24點 Pascal大暴力程式
- 分塊——優雅的暴力
- Linux防止SSH暴力破解Linux
- 迭代暴力破解域名工具
- 帶你瞭解 日本最大的安全會議 Code Blue
- 遭受網路暴力該如何應對?實名制可有效遏制網路暴力
- Global Blue:中國“精英”旅客數量佔全球比例的40%
- Global Blue :中國“精英”旅客佔全球總數近四成
- 3BLUE1BROWN 神經網路三講筆記神經網路筆記
- 暴力破解zip加密檔案加密
- 藍橋杯——暴力列舉篇
- POJ 1129 Channel Allocation (暴力搜尋)
- web類靶機暴力破解Web
- DVWA-Brute Force暴力破解
- fail2ban 防止暴力破AI
- “面對面”的網路暴力
- 《非暴力溝通》讀後感
- 悠星網路二次元新作 Blue Archive 上線二次元Hive