Skip to main content
  1. Posts/

poj 2503 Babelfish (字符串hash +sscanf读入技巧)

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给定一个两种语言的对照关系表…给出后一种语言中的单词,问对应的前一种语言的单词是什么。。。

思路:hash一下然后map存一下即可。。。。读入方式由于单词表和查询是根据空行分开的。。那么读入不能用scanf(因为会跳过空行),要用gets。。。然后再sscanf一下。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年11月20日 星期日 11时13分29秒
 4File Name :code/poj/2503.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34char eng[N][12],fori[N][12];
35char str[N];
36map<int,int>mp;
37unsigned int BKDRHash(char *str)
38{
39    unsigned int seed = 113;
40    unsigned int hash = 0 ;
41    while (*str) hash = hash*seed+(*str++);
42    return (hash&0x7fffffff);
43}
44
45int main()
46{
47	#ifndef  ONLINE_JUDGE
48	freopen("code/in.txt","r",stdin);
49  #endif
50    int cnt = 1 ;
51    while (gets(str))
52    {
53	if (strlen(str)==0) break;
54	sscanf(str,"%s %s",eng[cnt],fori[cnt]);
55	cnt++;
56    }
57    cnt--;
58 //   for ( int i = 1; i <= cnt ;i++) printf("%s %s\n",eng[i],fori[i]);
59    for ( int i = 1; i <= cnt ; i++) mp[BKDRHash(fori[i])] = i ;
60    while(scanf("%s",str)!=EOF)
61    {
62	if (!mp[BKDRHash(str)]) puts("eh");
63	else puts(eng[mp[BKDRHash(str)]]);
64    }
65
66
67  #ifndef ONLINE_JUDGE
68  fclose(stdin);
69  #endif
70    return 0;
71}

Related

hdu 1880 魔咒词典 (字符串hash)

·1 min
题目链接 题意:给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

uva 156 - Ananagrams

·2 mins
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92 题意:给出一段文字,包含若干个单词,以’#‘结束。按照字典序输出所有的ananagrams。所谓ananagram,是指经过任意的重排后,不能得到这段文字中的另一个单词(不区分大小写) 思路:首先是字符串的读入…可以整行读入然后用空格分隔单词。由于补区分大小写,所以要都转化成小写…但是输出的时候要输出原始,所以还记得保留一份。而且要能够通过新的找到原始的(我用了一个toori的map<string,string>来实现) 然后最关键的部分是如何判断两个单词经过重排是否能一样…

hdoj4391 Paint The Wall

·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意:有 n 个点,每个点有一种颜色(可能相同),两种操作:1、将区间 [a,b] 染成颜色 c ; 2、询问区间 [a,b] 中颜色为 c 的点有多少个。 思路:因为颜色种类很多。。。没办法通过建很多棵线段树解决。我们用分块的办法。。。