POJ(2503)(两种方法,二分,Map)

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
using namespace std;

const int INF = 0x3f3f3f3f;

struct node
{
    char str1[15];
    char str2[15];
}letter[100005];

bool cmp(node a, node b)
{
    return strcmp(a.str2,b.str2)<=0;
}


int  BinarySearch(node a[], int size, char* str)
{
    int left = 1;
    int right = size;
    int m = left + (right - left) / 2;
    while (left <= right)
    {
        if (strcmp(a[m].str2, str) == 0)
        {
            return m;
        }
        else if (strcmp(a[m].str2, str) < 0)
        {
            left = m + 1;
            m = left + (right - left) / 2;
        }
        else
        {
            right = m - 1;
            m = left + (right - left) / 2;
        }
    }
        return -1;
}
int main()
{
    char str[30];
    int k;
    k = 1;
    while (gets(str))
    {
        if (str[0] == '\0')
            break;
        sscanf(str,"%s%s", letter[k].str1, letter[k].str2);
        k++;
    }
    sort(letter + 1, letter + k,cmp);
    while (gets(str))
    {
        int index = BinarySearch(letter, k - 1, str);
        if (index == -1)
            printf("eh\n");
        else
            printf("%s\n", letter[index].str1);
    }
    return 0;
}

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<map>
using namespace std;

const int INF = 0x3f3f3f3f;

int main()
{
    char str1[15], str2[15],str[30];
    map<string,string> Map;
    while (gets(str))
    {
        if (strlen(str) == 0)
            break;
        sscanf(str, "%s %s", str1, str2);
        Map[str2] = str1;
    }
    string str3, temp;
    while (cin >> str3)
    {
        temp = Map[string(str3)];
        if (temp.length()== 0)
            cout << "eh" << endl;
        else
            cout << temp << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值