Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我的天啊,这简直就是天方夜谭,我这辈子第一次遇见那么水的数据真不是我装B,这是我写的KMP算法,pi数组(也即next数组)的初始化函数cal_prefix竟然没有调用都能AC了!!!这数据真是太奇葩了。我是在后来做3450的时候,拿此题代码改了改去提交一直WA,仔细检查才检查出来的。。。 附上我原来的奇葩代码 #include <cstdlib> #include <iostream> #include <stdio.h> #include <string.h> using namespace std; int pi[65]; //char s[65]; char pattern[65]; char DNA[12][65]; char ansDNA[65]; int len; void cal_prefix(int pi[],char pattern[]){ int k,m,q; pi[1] = 0; k = 0; //m = pattern.length(); m = strlen(pattern); for(q=2;q<m+1;q++){ while(k>0 && pattern[k+1-1]!=pattern[q-1]){ k = pi[k]; } if(pattern[k+1-1] == pattern[q-1]){ k = k+1; } pi[q] = k; } return; } int kmpPatternMatcher(char text[],char pattern[],int pos){ int res = -1; int n,m,i,q=0; //n = text.length(); m= pattern.length(); n = strlen(text); m=strlen(pattern); //int* pi = new int[m+2]; for(i=pos+1;i<=n;i++){ while(q>0 && pattern[q+1-1]!=text[i-1]){ q = pi[q]; } if(pattern[q+1-1]==text[i-1]){ q = q+1; } if(q==m){ res = i-m; break; //break; } } //delete []pi; //printf("%d\n",sum); return res; } bool check(char pattern[],int m){ int i; for(i=2;i<=m;i++){ if(kmpPatternMatcher(DNA[i],pattern,0)<0){ return false; } } return true; } int main(int argc, char *argv[]) { int n,m,i,j,k,l; scanf("%d",&n); //printf("%d\n",n); while(n--){ scanf("%d",&m); //printf("%d\n",m); for(i=1;i<=m;i++){ scanf("%s",DNA[i]); } /*for(i=1;i<=m;i++){ printf("%s\n",DNA[i]); } */ len = -1; for(i=1;i<=60;i++){//枚举字串的长度 for(j=0;j<=60-i;j++){//从j开始到j+i的字符串 for(k=j,l=0;k<j+i;k++,l++){ pattern[l]=DNA[1][k];//长度为i的字串 } pattern[l]='\0'; //printf("%s l=%d\n",pattern,l); //本来应该在这里cal_prefix(pi,pattern); if(check(pattern,m)){ if(l>len){ len = l; strcpy(ansDNA,pattern); }else if(l==len){ if(strcmp(pattern,ansDNA)<0){ strcpy(ansDNA,pattern); } } } } } if(len>=3){ printf("%s\n",ansDNA); }else{ printf("no significant commonalities\n"); } } //system("PAUSE"); return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator