题目:http://acm.hdu.edu.cn/showproblem.php?pid=1894
先排序,再进行判断是否是前缀,可以明显提高效率;
AC代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node{ char s[33]; }node; node w[50005]; int cmp(const void *a,const void *b){ struct node *c=(struct node *)a; struct node *d=(struct node *)b; return strcmp(c->s,d->s); } int main(){ int t,n,i,j,k,count,l; scanf("%d",&t); while(t--){ scanf("%d",&n); count=0;//计数; for(i=0;i<n;i++){ scanf("%s",w[i].s); } qsort(w,n,sizeof(w[0]),cmp); //for(i=0;i<n;i++){ // printf("%s/n",w[i].s); //} for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++){ l=strlen(w[i].s); for(k=0;k<l;k++){ if(w[i].s[k]!=w[j].s[k]){ break; } } if(k==l){ count++; } else{ break; } } } if(count<=11519){ printf("%d/n",count); } else{ printf("%d/n",count%11519); } } }