字符串二分查找
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"stdlib.h"
using namespace std;
char a[1001][22];
char ch[22];
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int midsearch(int left,int right)
{
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(ch,a[mid])==0)
return mid;
else if(strcmp(ch,a[mid])>0)
left=mid+1;
else
right=mid-1;
}
return -1;
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",a[i]);
qsort(a,n,sizeof(a[0]),cmp);
while(scanf("%s",ch)!=-1)
{
if(midsearch(0,n-1)!=-1)
printf("find!
");
else
printf("no find!
");
}
return 0;
}