POJ2503c++解法

POJ2503


#题目描述
##题解
题目如下:
Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.
Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output

cat
eh
loops
Hint

Huge input and output,scanf and printf are recommended.
Source

Waterloo local 2001.09.22

#include<iostream>
#include<cstring>
#define max 100001
using namespace std;
struct Ditionary
{
	char word1[12];
	char word2[12];
};
Ditionary d[max];//把结构体数组定义为全局变量,在下面的函数中就不用再传数组 
void QuickSort(int start,int end)//快排函数 ,升序排序 
{
	if(start>=end)return;//递归结束条件 
	int i=start,j=end;
    Ditionary temp=d[start];//先把第一个数作为基准值,记录起来 ,空出来一个地方 
    while(i<j)//循环条件,当i>=j时循环结束 
    {
    	while(strcmp(d[j].word2,temp.word2)>0&&j>i)j--;//从后面找一个比基准值小的数 
    	if(i<j)d[i++]=d[j]; //将找到的数填进空出来的地方 
    	while(strcmp(d[i].word2,temp.word2)<0&&i<j)i++;//从前面找到哟个比基准值大的数 
    	if(i<j)d[j--]=d[i];//将找到的数填进空出来的地方 
    
	}
    d[i]=temp;//将基准值填进数组中空出来的地方 
	QuickSort(start,j-1);//递归 
	QuickSort(j+1,end);
	
}
/*
二分查找法需要一个已经排好的数组
*/ 
int BinarySearch(char *a,int low,int high)//二分查找法 
{
	
	while(low<=high)
	{
		int mid=(low+high)/2;
		if(strcmp(a,d[mid].word2)==0)return mid;//找到就返回 数组下标 
		else if(strcmp(a,d[mid].word2)>0)low=mid+1;//当值比中间值大的时候,继续从后面找 
		else high=mid-1;//当值比中间值小的时候,继续从前面找 
	 }
	return -1;//没有找到的时候,就返回-1; 
}
int main()
{
	char str[255];
	int i=0;
	/*
	处理输入,因为输入结束的条件是输入一个空行,
	所以要用能读入空行的输入方式,在c++中用cin.getline(字符数组名,字符长度)
	*/ 
	while(cin.getline(str,255)) 
	{
	  if(str[0]=='\0')break;
		int p1=0,p2=0;
		while(str[p1]!=' ')//将空格前的赋值给结构体的第一个字符数组 
		{
		d[i].word1[p1]=str[p1];
		p1++;
     	}
     	
		d[i].word1[p1]='\0';//因为字符数组要以'\0'结束,所以要多加一个'\0' 
		p1++;
		while(str[p1]!='\0')
		{
			d[i].word2[p2]=str[p1];
			p2++;
			p1++;	
		}
		d[i].word2[p2]='\0';
	 
	     i++;
    }
	
	
	QuickSort(0,i-1);//排序 

	char a[12];
	while(cin>>a)
	{
	   int ref=	BinarySearch(a,0,i-1);//二分查找 
	   if(ref==-1)cout<<"eh"<<endl;//没找到就输出“eh" 
	   else cout<<d[ref].word1<<endl;//找打就输出结构体的第一个字符串 
	}
	return 0;	
}

结束语:第一次使用csdn写文章,不知道怎么用,只能写的比较简陋,其中的算法有什么错误,欢迎大家指正

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值