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 |
kmp+枚举 0ms因为开始没注意字典序输出,wrong了一次 贴代码: #include <iostream> using namespace std; #define LEN 60 int next[LEN]; char dna[11][LEN]; void get_next( char *a, int len ) { int i,temp; memset(next,0,sizeof(next)); for( i = 1; i < len; i++ ) { temp = next[i-1]; while( temp && a[i] != a[temp] ) temp = next[temp-1]; if( a[i] == a[temp] ) next[i] = temp+1; } } bool check( char *a, int len, int size ) { int i,j,k; get_next(a,len); for( k = 1; k < size; k++ ) { j = 0; for( i = 0; i < LEN; i++ ) { while( j > 0 && dna[k][i] != a[j] ) j = next[j-1]; if( dna[k][i] == a[j] ) { if( j == len-1 ) break; else j++; } else j = 0; } if( j < len-1 ) return false; } return true; } int main() { int cas,n; int i,j,len; bool flag; int start,l; char *temp; cin >> cas; while( cas-- ) { cin >> n; for( i = 0; i < n; i++ ){ cin >> dna[i]; } flag = false; len = LEN; while( len>=3 && !flag ) { j = 0; for( i = 0;;i++ ){ if( i+len <= LEN ){ if( check(&dna[0][i],len,n) ){ flag = true; if( j == 0 || strncmp(&dna[0][i],temp,len) < 0 ){ temp = &dna[0][i]; start = i; l = len; j++; } } } else break; } len--; } if(flag){ for( i = start; i < start+l; i++ ) cout << dna[0][i]; cout << endl; } else cout << "no significant commonalities" << endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator