• POJ 2503 Babelfish


    POJ_2503

    之前集训的时候做过这道题目的,这次想换个方法,但自己写的哈希函数毫不犹豫地挂掉了。

    于是重操旧刃,写了qsort+二分查找的代码。

    一定要找个时间系统学一下常见的哈希函数!

    #include<string.h>
    #include
    <stdlib.h>
    #include
    <stdio.h>
    char b[15],english[100010][15],other[100010][15];
    int r[100010];
    int cmp(const void *_p,const void *_q)
    {
    int *p=(int *)_p;
    int *q=(int *)_q;
    return strcmp(other[*p],other[*q]);
    }
    int main()
    {
    int i,j,k,n,mid,max,min,ok;
    n
    =0;
    while(1)
    {
    b[
    0]='\0';
    gets(b);
    if(b[0]=='\0')
    break;
    sscanf(b,
    "%s%s",english[n],other[n]);
    n
    ++;
    }
    for(i=0;i<n;i++)
    r[i]
    =i;
    qsort(r,n,
    sizeof(r[0]),cmp);
    while(scanf("%s",b)==1)
    {
    max
    =n;
    min
    =0;
    ok
    =0;
    while(1)
    {
    mid
    =(max+min)/2;
    k
    =strcmp(b,other[r[mid]]);
    if(k==0)
    {
    ok
    =1;
    break;
    }
    if(mid==min)
    break;
    if(k>0)
    min
    =mid;
    else
    max
    =mid;
    }
    if(ok)
    printf(
    "%s\n",english[r[mid]]);
    else
    printf(
    "eh\n");
    }
    return 0;
    }

      

  • 相关阅读:
    CodeForcesGym 100524A Astronomy Problem
    ZOJ 2567 Trade
    HDU 3157 Crazy Circuits
    CodeForcesGym 100212E Long Dominoes
    UVALive 6507 Passwords
    [转]
    java socket
    Spark RDD Operations(2)
    Spark cache 和 persist
    vim 基础命令
  • 原文地址:https://www.cnblogs.com/staginner/p/2153905.html
Copyright © 2020-2023  润新知