| ||||||||||
| 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 | |||||||||
trie树 WA 望指教单纯的WA,测试过题目中的数据,测试过题目中的数据,考虑了最后一句“按字母排序问题”。
#include<iostream>
#include<string.h>
using namespace std;
typedef struct _node{
int next[4];
int count;
}NODE;
NODE nodes[10000];
int top;
char max_s[70];
int max_l;
void init_node(int i){
int j;
for(j=0;j<4;j++)
nodes[i].next[j]=-1;
nodes[i].count=0;
}
int G(char c){
switch(c){
case 'A':return 0;
case 'T':return 1;
case 'G':return 2;
case 'C':return 3;
}
return -1;
}
void build_tree(char* s){
int i,j,p;
init_node(0);
top=1;
for(i=0;i<64;i++){
j=0;
p=0;
while(s[i+j] != '\0'){
if(nodes[p].next[G(s[i+j])] != -1){
p=nodes[p].next[G(s[i+j])];
}
else{
init_node(top);
nodes[p].next[G(s[i+j])]=top;
p=top;
top++;
}
j++;
}
}
}
void search_tree(char* s,int count){
int i,j,p,l;
char cur[70];
max_l=2; //最长字符串长度
max_s[0]='\0';
for(i=0;i<64;i++){
j=0;
p=0; //树指针
l=0; //当前匹配串长度
while(s[i+j] != '\0'){
if(nodes[p].next[G(s[i+j])] != -1 && nodes[nodes[p].next[G(s[i+j])]].count >= count-1){
p=nodes[p].next[G(s[i+j])];
nodes[p].count=count; //用count来控制“只走前人走过的路”
cur[l]=s[i+j];
l++;
}
else{
cur[l]='\0';
if(l>max_l || l==max_l && strcmp(max_s,cur)>0){
strcpy(max_s,cur);
max_l=l;
}
break;
}
j++;
}
}
}
int main(){
int case_count,m;
char str[70];
int i,j;
cin >> case_count;
for(i=1;i<=case_count;i++){
cin >> m;
cin >> str;
build_tree(str);
for(j=1;j<=m-1;j++){
cin >> str;
search_tree(str,j);
if(max_l <= 2)break;
}
if(max_l <= 2){
cout << "no significant commonalities" << endl;
}
else{
cout << max_s << 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