HDOJ1894 String Compare

本文提供了一种解决HDU 1894问题的有效方法。通过首先对输入字符串进行排序,然后检查每个字符串是否为另一个字符串的前缀来确定答案。此方法显著提高了算法效率。

题目: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); } } }

转载于:https://www.cnblogs.com/xiajun/archive/2011/03/22/2298661.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值