Hardwood Species

来源:互联网 发布:2016淘宝最近查的严 编辑:程序博客网 时间:2024/05/16 19:21

http://poj.org/problem?id=2418

#include<iostream>#include<string>using namespace std;#define STR_LINE 35#define SPACE 32struct Node{int cnt;bool isWord;Node* next[96];Node(){cnt = 0;isWord = false;memset(next,0,sizeof(next));}};void insert(char* word,Node* root){root->cnt++;while(*word != '\0'){int index = *word-SPACE;if(root->next[index] == NULL){root->next[index] = new Node();}root = root->next[index];word++;}root->cnt ++;root->isWord = true;}void search(char* word,Node* root,int total_cnt){char* p = word;if(root->isWord){printf("%s %.4lf\n",word,100*(float)root->cnt/(float)total_cnt);}for(int i = 0;i<96;i++){if(root->next[i] == NULL) continue;char tail[2];tail[0] = SPACE+i;tail[1] = '\0';char tmp[STR_LINE];strcpy(tmp,p);strcat(tmp,tail);search(tmp,root->next[i],total_cnt);}}int main(){Node* root = new Node();char a[35];char* tmp="";while(cin.getline(a,30)){insert(a,root);}search(tmp,root,root->cnt);return 0;}

1240K1735MS

原创粉丝点击