uva 10282 - Babelfish

简介:

 

     这题可以用map直接水过,但是那样就没有什么意思了。

 

     所以还是用hash表写了下,学习了一下hash链表的方法,其实就是做一个邻接表,但是这样在判断时还要比较生成hash值的string,而且如果hash函数没构造好,复杂度会大大增加,不过是一个解决冲突的好办法。

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdlib.h>
#define INF 1E9
using namespace std;
int head[1000000],next[100010];
string s[100010],f[100010];
int get_hash(string s)
{
    long long ans=0;
    for(int i=0;i<s.size();i++)
      ans=ans*33+s[i];
    return ans%999991;
}
int main()
{
    memset(head,-1,sizeof(head));
    string a,t;
    char ss;
    int cnt=1,now,u;
    s[0]="eh";
    while(cin>>a>>t)
    {
        s[cnt]=a;
        f[cnt]=t;
        now=get_hash(t);
        next[cnt]=head[now];//因为数据保证唯一,所以直接插入即可
        head[now]=cnt;
        cnt++;
        getchar();
        if((ss=getchar())=='\n')break;//判断有无遇到空行
        else ungetc(ss,stdin);
    }
    while(cin>>a)
    {
        now=get_hash(a);
        bool flag=1;
        for(u=head[now];u!=-1;u=next[u])
        {
            if(f[u]!=a)continue;
            cout<<s[u]<<endl;
            flag=0;
        }
        if(flag)cout<<s[0]<<endl;
    }
}


 

目录
相关文章
|
10月前
uva10152 ShellSort
uva10152 ShellSort
44 0
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
868 0
|
机器学习/深度学习
|
算法
UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494...
1545 0
|
存储 固态存储
|
机器学习/深度学习
uva 11538 Chess Queen
点击打开链接 题意:给定一个n*m的矩阵,问有多少种方法放置两个相互攻击的皇后?规定在同一行同一列和同对角线的能够相互攻击 思路: 1 先考虑同一行的情况,n行就有n种情况,每一行有m*(m-1)种,总的是n*m*(m-1); 2 考虑同...
802 0
uva 10730 - Antiarithmetic?
点击打开链接uva 10730 思路:枚举等差中项 分析: 1 给定一个n个数的序列判断是否有等差子序列 2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列 3 对于n
821 0
uva 11627 Slalom
点击打开链接uva 11627 思路:二分答案 分析: 1 给定S个滑雪板的速度,问是否可以找到一个滑板使得通过所有的门的时间最少,如果找不到输出IMPOSSIBLE 2 很明显的二分题目,我们知道了二分那应该怎么判断是否可以通过所有...
1050 0