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 |
错在那里呢?大牛看看你就知道了?帮帮我!#include <stdio.h> #include <malloc.h> #include <string.h> #define M 26 #define N 1000+10 char str1[N][30]; typedef struct Trie1 { struct Trie1 *next[M]; int val; }Trie; Trie *root; void init() { int i; root=(Trie*)malloc(sizeof(Trie)); for(i=0;i<M;i++) { root->next[i]=NULL; root->val=0; } } void bulit(Trie *T,char str2[]) { int i,id,length,j; Trie *p,*q; p=root; length=strlen(str2); for(i=0;i<length;i++) { id=str2[i]-'a'; if(p->next[id]==NULL) { q=(Trie*)malloc(sizeof(Trie)); for(j=0;j<M;j++) { q->next[j]=NULL; q->val=0; } p->next[id]=q; p->val++; p=q; } else { p->val++; p=p->next[id]; } } } void find(Trie *T,char str[]) { int i,id,length; Trie *p=T; length=strlen(str); printf("%s ",str); for(i=0;i<length;i++) { id=str[i]-'a'; p=p->next[id]; printf("%c",str[i]); if(p->val==1) break; } } int main() { int i,n=0; init(); while(scanf("%s",str1[n])!=EOF) { bulit(root,str1[n]); n++; } for(i=0;i<n;i++) { find(root,str1[i]); printf("\n"); } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator