POJ 2121 English-Number Translator (散列)
2017-08-16 10:37
302 查看
Inglish-Number Translator
Time Limit: 1000MS Memory Limit: 65536KTotal 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
sixnegative 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 2121 English-Number Translator
- poj2121
- poj2121 模拟
- poj 2121 字符串(英文数字->阿拉伯数字)
- poj2121
- poj 2121 Inglish-Number Translator 将英文翻译成罗马数字
- POJ 2121
- POJ 2121 Inglish-Number Translator
- poj1840Eqs【散列表】
- POJ-2121 Inglish-Number Translator-数字英译汉
- poj 1840 Eqs 哈希散列
- poj 2121 Inglish-Number Translator
- POJ 1186 hash 线性探测再散列
- POJ 2081 Recaman's Sequence G++ 散列表的范围是博友求出来的
- poj 2121 && poj 2403
- poj 2121-Inglish-Number Translator
- poj 2121——Inglish-Number Translator
- POJ 2121 Inglish-Number Translator 笔记
- poj 2121 Inglish-Number Translator
- 字符串处理,Poj(2121)