Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2417186
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类: C/C++

2009-12-30 10:05:56

#include 
#include 
#include 
using namespace std;

typedef struct _pair
{
    string word;
    int index;
}ST_PAIR;

vector dictionary;
vector storage;

class cmp
{
public:
    bool operator() (const ST_PAIR &a, const ST_PAIR &b)
    {
        return a.word < b.word;
    }
};

int bin_search(int front, int rear, string &target)
{
    int mid;
    while (front <= rear)
    {
        mid = (front + rear) / 2;
        if (dictionary[mid].word == target)
            return mid;
        if (dictionary[mid].word > target)
            rear--;
        else
            front++;
    }
    return -1;
}

int main(int argc, char *argv[])
{
    int n, i, m, index;
    ST_PAIR pair;
    string english, chinese;
    while (EOF != scanf("%d", &n))
    {
        for (i=0 ; i        {
            cin >> english >> chinese;
            pair.word = english;
            storage.push_back(chinese);
            pair.index = i;
            dictionary.push_back(pair);
        }
        sort(dictionary.begin(), dictionary.end(), cmp());
        vector::iterator iter;
        vector::iterator end = dictionary.end();
        /* search */
        cin >> m;
        for (i=0 ; i        {
            cin >> english;
            index = bin_search(0, n, english);
            if (-1 == index)
                cout << "I can't find out this word" << endl;
            else
                cout << storage[index] << endl;
        }
        dictionary.clear();
        storage.clear();
    }
}
阅读(6214) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~