您的位置:首页 > 其它

POJ 2503 Babelfish(STL、二分、字典树、哈希)

2017-08-09 20:58 489 查看

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign
4000
word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay


Sample Output

cat
eh
loops


题目大意

在给出字典数据时,由后边的数查找前面的数,然后给出检索条目,输出查找结果,若字典中不存在,输出“eh”。

实现方法

1、STL

C++能过,G++TLE

#include <iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
map<string,bool>exi;
map<string,string>mp;
int main()
{
char str1[11],str2[11];
char t;
while(true)
{
if((t=getchar())=='\n')
break;
str1[0]=t;
int i=1;
while(true)
{
t=getchar();
if(t==' ')
{
str1[i]='\0';
break;
}
else
str1[i++]=t;
}
cin>>str2;
getchar();
mp.insert(make_pair<string,string>(str2,str1));
exi.insert(make_pair<string,bool>(str2,true));
}
while(cin>>str1)
{
if(exi[str1])
cout<<mp[str1]<<endl;
else
cout<<"eh"<<endl;
}
return 0;
}


2、二分

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100007
#define maxl 17
struct node
{
char str1[maxl];
char str2[maxl];
}s[maxn];
int num=0;
bool cmp(node x,node y)
{
return strcmp(x.str2, y.str2) < 0;
}
int binary_find(char *str)
{
int l=0,r=num-1;
while(l<=r)
{
int m=(l+r)/2;
if(strcmp(s[m].str2,str)==0)
return m;
if(strcmp(s[m].str2,str)<0)
l=m+1;
else if(strcmp(s[m].str2,str)>0)
r=m-1;
}
return -1;
}

int main()
{
char str[maxl*2];
while(true)
{
gets(str);
if(str[0] == '\0')
break;
sscanf(str,"%s %s", s[num].str1, s[num].str2);
num++;
}
sort(s,s+num,cmp);
while(gets(str)!=NULL)
{
if(str[0] == '\0')
break;
int ans=binary_find(str);
if(ans==-1)
printf("eh\n");
else
printf("%s\n",s[ans].str1);
}
return 0;
}


3、字典树

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxl 17
#define maxn 100007
char str[maxl*2],ans[maxl],str1[maxl],str2[maxl];
struct trie
{
char aim[maxl];
trie *ne[27];
}*root;
trie *create()
{
trie *p=new trie;
for(int i=0;i<27;i++)
p->ne[i]=NULL;
strcpy(p->aim,"eh");
return p;
}
void make_tree(trie *p,char *s,char *e)
{
int len=strlen(s),k;
for(int i=0;i<len;i++)
{
k=s[i]-'a';
if(p->ne[k]==NULL)
p->ne[k]=create();
p=p->ne[k];
}
strcpy(p->aim,e);
}
void find_str(trie *p,char *f)
{
int len=strlen(f),k;
for(int i=0;i<len;i++)
{
k=f[i]-'a';
if(p->ne[k]==NULL)
{
strcpy(ans,"eh");
break;
}
p=p->ne[k];
}
strcpy(ans,p->aim);
}
int main()
{
root=create();
int i=0;
while(true)
{
gets(str);
if(str[0] == '\0')
break;
sscanf(str,"%s %s", str1, str2);
make_tree(root,str2,str1);
i++;
}
while(gets(str)!=NULL)
{
if(str[0] == '\0')
break;
find_str(root,str);
printf("%s\n",ans);
}
return 0;
}


4、哈希

ELFHash算法

unsigned int ELFHash(char *str)
{
unsigned int hash = 0;
unsigned int x = 0;

while (*str)
{
hash=(hash << 4);//将hash左移4位,等价于*16
hash = hash + (*str);
//加上当前字符位的ASCII码,即把当前字符ASCII存入hash低四位。
if ((x = hash & 0xF0000000L) != 0)
//取hash的高四位并赋值给x,如果不为0,则表明字符串的长度大于7,现在正在往hash中存入第7个字符,
//如果再不处理,在添加下一个字符hash左移4位时,曾存入的第一个字符(高4位)就将被移出,造成信息丢失
{
hash ^= (x >> 24);
//将hash的高四位与hash的低5~8位进行异或(算是通过影响5~8位来实现对信息的一种保存方式吧<个人理解>),
//因为hash低1~4刚刚存入了新的字符,所以是右移24位
//x右移24位时,如果最高的那一位为0,只会影响hash的5-8位,否则会影响5-31位,因为C语言使用的算数移位,
//最高位为1导致x右移时,会在前面补1
hash &= ~x;   //对hash的最高4位(28~31)清零,用于实现对下一个字符的存入
}
str++;  //遍历下一个字符
}
return (hash & 0x7FFFFFFF);
//如果最后结果是负数,处理为非负数,即舍弃最高位(当处理字符串时,只有字符不可能为负值)
}


代码实现

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100007
#define maxl 17
const int mod = maxn;
int hashTable[mod];
int countt;
char ans[maxl];
struct HashNode
{
char str1[maxl];
char str2[maxl];
int next;
};
HashNode h[maxn];
void init_Hash()
{
for (int i = 0; i < mod; i++)
{
hashTable[i] = -1;
}
countt = 0;
}
int ELFhash(char *str)
{
unsigned long hash = 0;
unsigned long x = 0;
while (*str)
{
hash = (hash << 4) + (*str++);
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return hash % mod;
}
void insert_Hash(char str1[],char str2[])
{
int value = ELFhash(str2);
strcpy(h[countt].str1,str1);
strcpy(h[countt].str2,str2);
h[countt].next = hashTable[value];
hashTable[value] = countt;
countt++;
}
bool search_Hash(char str[])
{
int value = ELFhash(str);
int next = hashTable[value];
while (next != -1)
{
if (strcmp(str, h[next].str2) == 0)
{
strcpy(ans,h[next].str1);
return true;
}
next = h[next].next;
}
return false;
}
int main()
{
init_Hash();
char str1[maxl],str2[maxl];
char str[maxl*2];
while(true)
{
gets(str);
if(str[0] == '\0')
break;
sscanf(str,"%s %s", str1, str2);
insert_Hash(str1,str2);
}
while (gets(str)!=NULL)
{
if(str[0] == '\0')
break;
if (search_Hash(str))
printf("%s\n",ans);
else
printf("eh\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: