POJ 2503

2503

在这里插入图片描述

解决方法一:

用 map容器,一对一的映射过去。

要点二、需要注意输入的问题,这里有一行空行怎么保留,getline(cin, line); 这里读入line之后,就可以发现如果是空行,则line =“”

对输入的字符串进行截取。

#include<iostream>
#include <map>
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int main()
{
    map<string, string> dict;
    string line = "", value = "", key = "";
    int v_pos = 0, k_pos = 0;
    while (1)
    {
        getline(cin, line);
        if (line == "")
            break;
        v_pos = line.find(' ');  //查找第一个空格位置
        value = line.substr(0, v_pos); //取子串
        k_pos = line.rfind(' '); //找最后一个空格位置
        key = line.substr(v_pos + 1, k_pos - v_pos - 1); //取子串
        dict.insert(pair<string, string>(key, value));
    }
    char query[100];
    while (~scanf("%s", query))
    {
        if (dict.find(query) != dict.end())
            cout << dict[query] << endl;
        else
            cout << "eh" << endl;
    }
    return 0;
}

方法二、用二分查找和qsort解决

这里对字符串进行比较。
按字典序排序。
这样可以进行二分查找。

//按字典序排序 
int cmp_chars(const void* e1, const void* e2)
{
	return strcmp(*(char**)e1, *(char**)e2);
}
void test2()
{
	 char* arr1 = "bc";
	 char* arr2 = "ad";
	 char* arr3 = "ac";
	 char* p[3] = { arr1,arr2,arr3 };
	int sz = sizeof(p) / sizeof(p[0]);
	qsort(p, sz, sizeof(p[0]), cmp_chars);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s\n", p[i]);
	}
}
题解:

#include <iostream>
# include <stdio.h>
#include<string.h>
#include<stdlib.h> 
const int MAX = 100001;
typedef struct
{
	char e[11];
	char f[11];
}Entry;
Entry entry[MAX];
int i = 0;		//词典总条数

int cmp(const void* a, const void* b)
{
	return strcmp((*(Entry*)a).f, (*(Entry*)b).f);
}
int BinSearch(char c[])
{
	int low = 0, high = i - 1;
	int mid, t;
	while (low <= high)
	{
		mid = (low + high) / 2;
		t = strcmp(entry[mid].f, c);  //字符匹配 和map的find功能一样。 
		if (t == 0)
			return mid; 
		else if (t == -1)  //小了
			low = mid + 1;
		else
			high = mid - 1;
	}
	return -1;
}

int main()
{
	char str[25];
	int index = -1;
	while (gets(str))
	{
		if (str[0] == '\0')
			break;
		sscanf(str, "%s%s", entry[i].e, entry[i].f);
		i++;
	}
	qsort(entry, i, sizeof(Entry), cmp);
	while (gets(str))
	{
		index = BinSearch(str);
		if (index == -1)
			printf("eh\n");
		else
			printf("%s\n", entry[index].e);
	}
	
	return 0;
}

用vector。

解决binary_search只能判断是否找到,不能判断位置。

lower_bound(dictvec.begin(), dictvec.end(), word, wordcmp); //返回当前开始,后面第一个大于等于word的单词
bool res = binary_search(dictvec.begin(),dictvec.end(),word ,wordcmp); //查找效率高,返回是否找到该值。
// 需要选择 C++ 语言 而不是选择 G++
#include <iostream>  
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
struct  node
{  
  string word1;  
  string word2;  
}word;  

vector<node> dictvec;
vector<node>::iterator it;

bool wordcmp(node val1, node val2)
{
	return val1.word2.compare(val2.word2)<0;  //string1.compare(string2)相同则为0,不同则小于0 
}

int main()  
{  
    string str;  
	int pos1=0;
	char buf[255];

   while(cin.getline(buf,255))
    {  
	if(buf[0] == '\0')  
        break;  

	pos1=0;	string str1 ;	string str2 ;

        while(buf[pos1]!=' ')
	       str1+=buf[pos1++];
		
	    pos1++;
        while(buf[pos1]!='\0')
	        str2+=buf[pos1++];

        word.word1=str1;
	    word.word2=str2;  
        dictvec.push_back(word);

    }  
    sort(dictvec.begin(),dictvec.end(),wordcmp); //sort(begin, end, cmp),  
	//begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数为排序准则

    while(cin.getline(buf,255))  
	{
		word.word2 = buf;
        bool res = binary_search(dictvec.begin(),dictvec.end(),word ,wordcmp);   //查找效率高,返回是否找到该值。
        if(!res )  
            cout<<"eh"<<endl;  
		else
		{
			it = lower_bound(dictvec.begin(), dictvec.end(), word, wordcmp);    //返回当前开始,后面第一个大于等于word的单词
			cout << it ->word1<<endl;	//输出该键		
		}
		
    }  
    return 0;  
}  

不合格的方法

// 需要选择 C++ 语言 而不是选择 G++
#include <iostream>  
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

struct  node
{  
  string word1;  
  string word2;  
}word;  

vector<node> dictvec;
bool wordcmp(node val1, node val2){
	return val1.word2.compare(val2.word2)<0;
}

int search(string key){
	int low = 0, high = dictvec.size();
	int mid, res;
	while (low <= high)
	{
		mid = (low + high) / 2;
		res = dictvec[mid].word2.compare(key);
		if (res == 0)//找到了
			return mid;
		else if (res == -1)
			low = mid + 1;
		else
			high = mid - 1;
	}
	return -1;
}

  
int main()  {  
    string str;  
	int pos1=0;
	char buf[255];

   while(cin.getline(buf,255))
    {  
	if(buf[0] == '\0')  
        break;  
	pos1=0;	string str1 ;	string str2 ;
        while(buf[pos1]!=' ')
	       str1+=buf[pos1++];
	    pos1++;
        while(buf[pos1]!='\0')
	        str2+=buf[pos1++];
        word.word1=str1;
	    word.word2=str2;  
        dictvec.push_back(word);
    }  
    sort(dictvec.begin(),dictvec.end(),wordcmp); 
	while (cin.getline(buf, 255))
	{
		word.word2 = buf;
		int res = search(word.word2);
		if (res==-1)
			cout << "eh" << endl;
		else
		{			
			cout << dictvec[res].word1<< endl;}	
    }  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值