Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

二分家快排

Posted by chenxuan123456789 at 2012-07-30 19:49:06 on Problem 2503
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 100000+10
typedef struct node
{
	char s1[15];
	char s2[15];
}Node;
Node word[M];
int n;
int cmp(const void *_a,const void *_b)
{
	Node *a=(Node*)_a;
	Node *b=(Node*)_b;
	return strcmp(a->s2,b->s2);
}
void find(char s[])
{
	int low=0,high=n,mid,flag;
	flag=0;
	while(low<=high)
	{
      mid=(low+high)/2;
	  if(!strcmp(s,word[mid].s2))
	  {
		  flag=1;
		  printf("%s\n",word[mid].s1);
		  break;
	  }
	  else
	  if(strcmp(s,word[mid].s2)>0)
	   low=mid+1;
	  else
		if(strcmp(s,word[mid].s2)<0)
		high=mid-1;
	}
	if(!flag)
	printf("eh\n");
}
int main()
{
 
	char query[15];
	char a[30],b[15],c[15];
	while(gets(a)&&a[0]!='\0')
	{
		sscanf(a,"%s%s",&b,&c);
		strcpy(word[n].s1,b);
		strcpy(word[n++].s2,c);
	}
	qsort(word,n,sizeof(Node),cmp);
	while(gets(a)&&a[0]!='\0')
	{
		sscanf(a,"%s",&query);
		find(query);
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator