Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9754 | Accepted: 4130 |
Description
Input
Output
Sample Input
carbohydrate cart carburetor caramel caribou carbonic cartilage carbon carriage carton car carbonate
Sample Output
carbohydrate carboh cart cart carburetor carbu caramel cara caribou cari carbonic carboni cartilage carti carbon carbon carriage carr carton carto car car carbonate carbona
Source
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 char str[10100][22]; 6 7 struct node 8 { 9 int cnt; 10 node *next[26]; 11 }*p; 12 13 void build(char str[],int k,node *head) 14 { 15 while(k<strlen(str)) 16 { 17 if(head->next[str[k]-'a']!=NULL) 18 { 19 head->next[str[k]-'a']->cnt+=1; 20 head=head->next[str[k]-'a']; 21 } 22 else 23 { 24 head->next[str[k]-'a']=new node; 25 head=head->next[str[k]-'a']; 26 head->cnt=1; 27 for(int i=0;i<26;i++) 28 head->next[i]=NULL; 29 } 30 k++; 31 } 32 } 33 34 void search(char str[],int k,node *head) 35 { 36 while(k<strlen(str)) 37 { 38 if(head->next[str[k]-'a']!=NULL) 39 { 40 printf("%c",str[k]); 41 if(head->next[str[k]-'a']->cnt==1) 42 return; 43 } 44 head=head->next[str[k]-'a']; 45 k++; 46 } 47 } 48 49 void del(node *head) 50 { 51 if(head==NULL) 52 return; 53 for(int i=0;i<26;i++) 54 { 55 del(head->next[i]); 56 head->next[i]=NULL; 57 } 58 delete(head); 59 return; 60 } 61 62 int main() 63 { 64 p=new node; 65 for(int i=0;i<26;i++) 66 p->next[i]=NULL; 67 int cnt=0; 68 while(~scanf("%s",str[cnt])) 69 { 70 build(str[cnt],0,p); 71 cnt++; 72 } 73 for(int i=0;i<cnt;i++) 74 { 75 printf("%s ",str[i]); 76 search(str[i],0,p); 77 printf("\n"); 78 } 79 del(p); 80 return 0; 81 }