您的位置:首页 > 其它

poj 2503 Babelfish

2015-02-06 10:15 260 查看
原题:http://poj.org/problem?id=2503

简单的快排+二分搜索

具体实现如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  N 1000000 + 10
typedef char State[11];
typedef struct tmp
{
    State Eng;
    State Fori;
}Node;
Node rec
;
int BinSearch(const char *target,int end)
{
    int low=0,high=end,mid;
    while(low<=high)
    {
        mid=(low+high)>>1;
        if(0 == strcmp(rec[mid].Fori,target))
            return mid;
        else if(-1 == strcmp(rec[mid].Fori,target))
            low=mid+1;
        else
            high=mid-1;
    }
    return -1;
}
int cmp(const void *a,const void *b)
{
    Node *p1=(Node *)a,*p2=(Node *)b;
    return strcmp(p1->Fori,p2->Fori);
}
void solve(char *buf,int count)
{
    int index=BinSearch(buf,count);
    if(-1 == index)
        puts("eh");
    else
        printf("%s\n",rec[index].Eng);
}
int main()
{
    int count=0;
    State buf;
    while(gets(buf) && '\0'!=buf[0])
    {
        State tp1,tp2;
        sscanf(buf,"%s %s",tp1,tp2);
        strcpy(rec[count].Eng,tp1);
        strcpy(rec[count++].Fori,tp2);
    }
    qsort(rec,count,sizeof(rec[0]),cmp);
    while(gets(buf))
        solve(buf,count);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: