一道相对简单又经典的题目,可以用多种方法进行求解。
1、直接用stl map解决,水过。不过效率相对有差些。 907Ms
/*poj2503, wrote by Dream Chen 2011/4/1*/ #include <iostream> #include <string> #include <map> using namespace std; map<string,string> map_word; void GetWord(const string &srcline, string &first, string &second); int main() { string line; while (getline(cin,line)) { string first; string second; if (0 == line.length()) { break; } GetWord(line, first, second); map_word[second] = first; } while(getline(cin, line)) { int i = map_word.count(line); if (i > 0) { cout << map_word[line] << endl; } else { cout << "eh" << endl; } } return 0; } void GetWord(const string &srcline, string &first, string &second) { int i = srcline.find_first_of(' '); first = srcline.substr(0,i); int j = srcline.find_first_not_of(' ', i); second = srcline.substr(j, srcline.length() - j); }
2、用一个二维数据的完全hash. 563MS
/* Poj 2503, Wrote by Dream, 2011/4/24*/
#include <iostream>
using namespace std;
const int PRIME = 13131;
int hash[PRIME][131];
char foreignword[100010][12];
char englishword[100010][12];
void Initial();
bool readWordfromstdin(char *word);
int gethash(char *word);
void sethash(int i, int h);
int translate(char *dst, char *src);
int main()
{
Initial();
int i = 1;
while(readWordfromstdin(englishword[i]))
{
readWordfromstdin(foreignword[i]);
sethash(gethash(foreignword[i]), i);
++i;
}
char tmpforword[12];
while(readWordfromstdin(tmpforword))
{
translate(tmpforword, tmpforword);
printf("%s/n", tmpforword);
}
return 0;
}
void Initial()
{
memset((void*)foreignword, 0, sizeof(foreignword));
memset((void*)englishword, 0, sizeof(englishword));
memset((void*)hash, 0, sizeof(hash));
}
int translate(char *dst, char *src)
{
int h = gethash(src);
int j = 0;
while(hash[h][j] != 0 && strcmp(foreignword[hash[h][j]], src)!= 0 && j < PRIME)
++j;
if (hash[h][j] == 0 || j >= PRIME)
{
strcpy(dst, "eh");
return false;
}
strcpy(dst, englishword[hash[h][j]]);
return true;
}
void sethash(int i, int h)
{
int j = 0;
while(hash[i][j] != 0)
++j;
hash[i][j] = h;
}
bool readWordfromstdin(char *word)
{
char ch = '/0';
ch = getchar();
if ('/n' == ch || ch == EOF)
{
return false;
}
*word = ch;
++word;
while((ch = getchar())!=' ' && ch != '/n' && ch != EOF)
{
*word++ = ch;
}
*word = '/0';
return true;
}
int gethash(char *word)
{
int h = 0;
while(*word != '/0')
{
h = (h * 26 + *word - 'a') % PRIME;
++word;
}
return h;
}
3、用Tried树进行单词的存储与查找,可以较高地提高速度,不过消耗内在较大,附代码,250Ms水过
/* poj2503, written by Dream, 2011/4/25 */
#include<iostream>
#include<cstring>
using namespace std;
const int Max = 260050;
const int branchNum = 26;
struct TriedNode
{
char re[12];
TriedNode *branch[branchNum];
};
TriedNode root;
TriedNode node[Max];
int p = 0;
void Initial()
{
memset((void*)node, 0, sizeof(node));
}
void Insert(char *word, char *re)
{
if (NULL == word)
{
return;
}
TriedNode *location = &root;
while(*word)
{
if (NULL == location->branch[*word - 'a'])
{
location->branch[*word - 'a'] = &node[p++];
}
location = location->branch[*word - 'a'];
++word;
}
strcpy(location->re, re);
}
void Search(char *res, char *word)
{
if (NULL == word)
{
return;
}
TriedNode *location = &root;
while(*word)
{
if (location->branch[*word - 'a'])
{
location = location->branch[*word - 'a'];
}
else
{
strcpy(res, "eh");
return;
}
++word;
}
strcpy(res, location->re);
}
int main()
{
char word[12];
char eng[12];
char res[12];
char ch = '/0';
Initial();
while(scanf("%s%s", eng,word) != EOF)
{
Insert(word, eng);
getchar();
ch = getchar();
if ('/n' != ch)
{
ungetc(ch, stdin);
continue;
}
break;
}
while(scanf("%s", word) != EOF)
{
Search(res, word);
printf("%s/n", res);
}
return 0;
};