您的位置:首页 > 编程语言 > C语言/C++

POJ 2121 English-Number Translator (散列)

2017-08-16 10:37 302 查看

Inglish-Number Translator

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 5514 Accepted: 2169

Description

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:

negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million

Input

The input consists of several instances. Notes on input:

Negative numbers will be preceded by the word negative.

The word “hundred” is not used when “thousand” could be. For example, 1500 is written “one thousand five hundred”, not “fifteen hundred”.

The input is terminated by an empty line.

Output

The answers are expected to be on separate lines with a newline after each.

Sample Input

six

negative seven hundred twenty nine

one million one hundred one

eight hundred fourteen thousand twenty two

Sample Output

6

-729

1000101

814022

Source

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>

//算出每一部分的值相加

char words[32][20] = {"negative", "zero", "one", "two", "three", "four","five",
"six", "seven", "eight", "nine","ten", "eleven", "twelve", "thirteen",
"fourteen","fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety",
"hundred", "thousand", "million"};
int values[32] = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90, 100, 1000, 1000000};
int cnt_words = 0;

struct node{
char word[20];
int value;
node(char * w, int v){
strcpy(word, w);
value = v;
}
};

node* hashTable[10001];

int hashCode(char * str) {
int code = 0;
auto i = 0;
while(str[i]) {
code = 107*(code + str[i++])%10001;
}
if(code < 0) code  += 10001;
return code;
}

void hash(char * str) {
int code = hashCode(str);
int oldCode = code;
int i = 0;
while(hashTable[code]) {
++i;
code = oldCode + i*i;
}
hashTable[code] = new node(str, values[cnt_words++]);
}

int find(char * str) {
int ret = 0;
int code = hashCode(str);
int oldCode = code;
int i = 0;
while(hashTable[code] && strcmp(str, hashTable[code]->word) != 0)
code = code + i * i;
return hashTable[code]->value;
}

int main() {
char str[400];
for(int i = 0; i < 10001; ++i)
hashTable[i] = nullptr;
for(auto i = 0; i < 32; ++i){
hash(words[i]);
}
char word[20];
int t = 0;
while(gets(str) && strcmp(str, "") != 0)
{
int i = strlen(str);
int rank = 1;
int ans = 0;
while(--i >= 0) {
if(isblank(str[i]))
{
int c = i + 1;
while(str[c] && !isblank(str[c]))
{   word[t++] = str[c++]; }
word[t] = '\0';
t = 0;
int v = find(word);
if(v == 100 || v == 1000 || v == 1000000 || v == -1)
{
if (v > rank)
rank = v;
else
rank *= v;
}
else {
ans += v*rank;
}
}
}
int c = i + 1;
while(str[c] && !isblank(str[c]))
{   word[t++] = str[c++]; }
word[t] = '\0';
t = 0;
int v = find(word);
if(v == -1)
{
ans *= v;
}
else {
ans += v*rank;
}
printf("%d\n", ans);
}
return 0;
}


CTU Open 2004,UVA 486

这题我用了散列,虽然比起暴力来说代码比较难写,但是胜在程序速度快啊。

a776
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj c++