字典树应用!(小技巧:先预处理,将大写转化为小写字母) #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node { int h; struct node * next[26]; }*tree; tree tt; int cnt; void getcnt(char a[]) { tree p,q;p=tt; int i=0,k; while(a[i]) { k=a[i]-'a'; if(p->next [k]==NULL) { q=(tree)malloc(sizeof(node)); memset(q->next ,NULL,sizeof(q->next )); q->h =-1; p->next [k]=q; } p=p->next [k]; i++; } if(p->h==-1) p->h =++cnt; } int main() { int n,i;char a[30]; while(scanf("%d",&n)!=EOF) { cnt=0; tt=(tree)malloc(sizeof(node)); memset(tt->next ,NULL,sizeof(tt->next )); tt->h =-1; while(n--) { scanf("%s",&a); for(i=0;i<strlen(a);i++) if('A'<=a[i]&&a[i]<='Z') a[i]=a[i]-'A'+'a'; getcnt(a); } printf("%d/n",cnt); } return 0; }