11158. Numbers and Words
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Once upon a time in a land far far away, inhabited only by math students, Iva and Vedran were discussing self-explanatory sentences. A part of these sentences is exactly one number and it is equal to the total number of letters in the sentence. Some examples are: “This sentence has thirtyone letters.”, “Blah blah seventeen”.
Little Jurica overheard his friends' conversation and decided to impress them with the amount of selfexplanatory sentences he knows by heart. He rushed back home and wrote a programme which will, given a sentence, tell him the minimum number he can put inside so that the sentence is valid. Unfortunately, his computer broke and now he needs your help. Write a programme to help Jurica!
The form of the sentence is: word1 word2 word3 … $ word_n-1 word_n. The character $ represents the place where the number should be put in.
For example, the form of the sentence “this sentence has thirtyone letters” would be “this sentence has $ letters”.
The rules that apply to writing numbers are the following:
· numbers from 1 to 10 are written “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, “ten”, respectively
· numbers from 11 to 19 are written “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”
· the remaining double digit numbers are written in a way that we name the tens' digit and add to it the name of the one digit remaining when we remove the tens' digit. Specially, if by removing the tens' digit we remain with zero, we add nothing to it
· the tens' digits (respectively from 2 to 9) are named the following: “twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety”
· three digit numbers are written in a way that we name the hundreds' digit number and add to it the number of the double digit number remaining. Specially, if by removing the hundreds' digit we remain with zero, we add nothing to it
· the hundreds' digits (respectively from 1 to 9) are named the following: “onehundred”, “twohundred”, “threehundred”, “fourhundred”, “fivehundred”, “sixhundred”, “sevenhundred”, “eighthundred”, “ninehundred”
· the rules that apply for numbers with more than three digits are not relevant because the input data will always be such that the output is less than a thousand
Examples of naming some numbers:
· 68 = “sixty” + “eight” = “sixtyeight”
· 319 = “threehundred” + “nineteen” = “threehundrednineteen”
· 530 = “fivehundred” + “thirty” = “fivehundredthirty”
· 971 = “ninehundred” + “seventy” + “one” = “ninehundredseventyone”
Input
The first line of input contains the integer N (1 ≤ N ≤ 20), the number of words in the sentence.
Each of the following N lines contains a word not longer than 50 lowercase letters of the English alphabet or the character $ (none of the words will be the name of a number).
The character $ will appear exactly once.
Output
The first and only line of output must contain the required sentence.
The numbers are named as mentioned before, even if the sentence sounds gramatically incorrect.
The input data will be such that a solution will always exist and is less than 1000.
You should output exact one space after each word.
Sample Input
样例1: 5 this sentence has $ letters 样例2: 7 $ is the number of letters here 样例3: 5 the letters are $ potato
Sample Output
样例1: this sentence has thirtyone letters 样例2: thirty is the number of letters here 样例3: the letters are twentynine potato
Hint
Clarification of the second example: Sentence is split in two lines because of the lack of space in the table. The total number of letters in the sentence is 6 + 2 + 3 + 6 + 2 + 7 + 4 = 30
Clarification of the third example: As you can see, this sentence is gramatically incorrect. Nevertheless, Jurica is not concerned by that, for he is a mathematician, not a linguist.
Problem Source
2014年每周一赛第五场
// Problem#: 11158
// Submission#: 3023001
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;
#define MAX_N 1005
string num[MAX_N];
string tenandunitPos(int i) {
vector<string> one2nineteen(20);
one2nineteen[0] = "";
one2nineteen[1] = "one";
one2nineteen[2] = "two";
one2nineteen[3] = "three";
one2nineteen[4] = "four";
one2nineteen[5] = "five";
one2nineteen[6] = "six";
one2nineteen[7] = "seven";
one2nineteen[8] = "eight";
one2nineteen[9] = "nine";
one2nineteen[10] = "ten";
one2nineteen[11] = "eleven";
one2nineteen[12] = "twelve";
one2nineteen[13] = "thirteen";
one2nineteen[14] = "fourteen";
one2nineteen[15] = "fifteen";
one2nineteen[16] = "sixteen";
one2nineteen[17] = "seventeen";
one2nineteen[18] = "eighteen";
one2nineteen[19] = "nineteen";
vector<string> ten(10);
ten[0] = "twenty";
ten[1] = "thirty";
ten[2] = "forty";
ten[3] = "fifty";
ten[4] = "sixty";
ten[5] = "seventy";
ten[6] = "eighty";
ten[7] = "ninety";
i = i % 100;
if (i == 0) return "";
if (i < 20) {
return one2nineteen[(i)];
} else {
return ten[i / 10 - 2] + one2nineteen[i % 10];
}
}
string hundredPos(int i) {
i = i / 100;
if (i == 0) return "";
return tenandunitPos(i) + "hundred";
}
void makeNum() {
for (int i = 0; i < MAX_N; i++) {
num[i] = hundredPos(i) + tenandunitPos(i);
}
}
int main() {
std::ios::sync_with_stdio(false);
makeNum();
int n;
cin >> n;
vector<string> s(n + 5);
int now = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
if (s[i][0] != '$') now += s[i].size();
}
for (int i = now + 1; i < now + 100 && i < 1000; i++) {
if (now + num[i].size() == i) {
for (int j = 0; j < n; j++) {
if (s[j] != "$") {
cout << s[j] << " ";
} else {
cout << num[i] << " ";
}
}
cout << endl;
break;
}
}
//getchar();
//getchar();
return 0;
}