import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
HashMap<String,Integer> lessThan1000=getLessThan1000();
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line);
String[]arr=new String[st.countTokens()];
for(int i=0;i<arr.length;i++){
arr[i]=st.nextToken();
}
sb.append(convToNum(arr, lessThan1000));
sb.append('\n');
}
System.out.print(sb);
}
static int convToNum(String[]arr,HashMap<String,Integer> hm){
int val=0;
LinkedList<String> digit=new LinkedList<String>();
LinkedList<String> thousand=new LinkedList<String>();
LinkedList<String> million=new LinkedList<String>();
int cases=0;
boolean neg=false;
for(int i=arr.length-1;i>-1;i--){
if(arr[i].equals("thousand")){
cases=1;
}
else if(arr[i].equals("million")){
cases=2;
}else{
if(i==0){
if(arr[0].equals("negative")){
neg=true;
continue;
}
}
if(cases==0){
digit.add(arr[i]);
}else if(cases==1){
thousand.add(arr[i]);
}else{
million.add(arr[i]);
}
}
}
val=getVal(million, hm)*1000*1000+getVal(thousand, hm)*1000+getVal(digit, hm);
if(neg){
val=-val;
}
return val;
}
static int getVal(LinkedList<String> list,HashMap<String,Integer> hm){
boolean first=true;
StringBuilder sb=new StringBuilder();
if(list.isEmpty()){
return 0;
}
while(!list.isEmpty()){
if(!first){
sb.append(' ');
}
sb.append(list.removeLast());
first=false;
}
return hm.get(sb.toString());
}
static HashMap<String, Integer> getLessThan1000(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
HashMap<String,Integer> digits0=initDigits0();
HashMap<String,Integer> digits1=initDigits1();
HashMap<String,Integer> digits2=initDigits2();
for(String s:digits0.keySet()){
hm.put(s,digits0.get(s));
}
for(String s:digits1.keySet()){
hm.put(s,digits1.get(s));
}
digits0.remove("zero");
for(String s:digits2.keySet()){
hm.put(s,digits2.get(s));
for(String s2:digits0.keySet()){
hm.put(s+" "+s2,digits2.get(s)+digits0.get(s2));
}
}
for(String s0:digits0.keySet()){
hm.put(s0+" hundred",digits0.get(s0)*100);
for(String s:digits0.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits0.get(s));
}
for(String s:digits1.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits1.get(s));
}
for(String s:digits2.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits2.get(s));
for(String s2:digits0.keySet()){
hm.put(s0+" hundred "+s+" "+s2,digits0.get(s0)*100+digits2.get(s)+digits0.get(s2));
}
}
}
return hm;
}
static HashMap<String, Integer> initDigits0(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("zero",0);
hm.put("one",1);
hm.put("two",2);
hm.put("three",3);
hm.put("four",4);
hm.put("five",5);
hm.put("six",6);
hm.put("seven",7);
hm.put("eight",8);
hm.put("nine",9);
return hm;
}
static HashMap<String, Integer> initDigits1(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("ten",10);
hm.put("eleven",11);
hm.put("twelve",12);
hm.put("thirteen",13);
hm.put("fourteen",14);
hm.put("fifteen",15);
hm.put("sixteen",16);
hm.put("seventeen",17);
hm.put("eighteen",18);
hm.put("nineteen",19);
return hm;
}
static HashMap<String, Integer> initDigits2(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("twenty",20);
hm.put("thirty",30);
hm.put("forty",40);
hm.put("fifty",50);
hm.put("sixty",60);
hm.put("seventy",70);
hm.put("eighty",80);
hm.put("ninety",90);
return hm;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
String line;
HashMap<String,Integer> lessThan1000=getLessThan1000();
while ((line = in.readLine()) != null) {
StringTokenizer st=new StringTokenizer(line);
String[]arr=new String[st.countTokens()];
for(int i=0;i<arr.length;i++){
arr[i]=st.nextToken();
}
sb.append(convToNum(arr, lessThan1000));
sb.append('\n');
}
System.out.print(sb);
}
static int convToNum(String[]arr,HashMap<String,Integer> hm){
int val=0;
LinkedList<String> digit=new LinkedList<String>();
LinkedList<String> thousand=new LinkedList<String>();
LinkedList<String> million=new LinkedList<String>();
int cases=0;
boolean neg=false;
for(int i=arr.length-1;i>-1;i--){
if(arr[i].equals("thousand")){
cases=1;
}
else if(arr[i].equals("million")){
cases=2;
}else{
if(i==0){
if(arr[0].equals("negative")){
neg=true;
continue;
}
}
if(cases==0){
digit.add(arr[i]);
}else if(cases==1){
thousand.add(arr[i]);
}else{
million.add(arr[i]);
}
}
}
val=getVal(million, hm)*1000*1000+getVal(thousand, hm)*1000+getVal(digit, hm);
if(neg){
val=-val;
}
return val;
}
static int getVal(LinkedList<String> list,HashMap<String,Integer> hm){
boolean first=true;
StringBuilder sb=new StringBuilder();
if(list.isEmpty()){
return 0;
}
while(!list.isEmpty()){
if(!first){
sb.append(' ');
}
sb.append(list.removeLast());
first=false;
}
return hm.get(sb.toString());
}
static HashMap<String, Integer> getLessThan1000(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
HashMap<String,Integer> digits0=initDigits0();
HashMap<String,Integer> digits1=initDigits1();
HashMap<String,Integer> digits2=initDigits2();
for(String s:digits0.keySet()){
hm.put(s,digits0.get(s));
}
for(String s:digits1.keySet()){
hm.put(s,digits1.get(s));
}
digits0.remove("zero");
for(String s:digits2.keySet()){
hm.put(s,digits2.get(s));
for(String s2:digits0.keySet()){
hm.put(s+" "+s2,digits2.get(s)+digits0.get(s2));
}
}
for(String s0:digits0.keySet()){
hm.put(s0+" hundred",digits0.get(s0)*100);
for(String s:digits0.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits0.get(s));
}
for(String s:digits1.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits1.get(s));
}
for(String s:digits2.keySet()){
hm.put(s0+" hundred "+s,digits0.get(s0)*100+digits2.get(s));
for(String s2:digits0.keySet()){
hm.put(s0+" hundred "+s+" "+s2,digits0.get(s0)*100+digits2.get(s)+digits0.get(s2));
}
}
}
return hm;
}
static HashMap<String, Integer> initDigits0(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("zero",0);
hm.put("one",1);
hm.put("two",2);
hm.put("three",3);
hm.put("four",4);
hm.put("five",5);
hm.put("six",6);
hm.put("seven",7);
hm.put("eight",8);
hm.put("nine",9);
return hm;
}
static HashMap<String, Integer> initDigits1(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("ten",10);
hm.put("eleven",11);
hm.put("twelve",12);
hm.put("thirteen",13);
hm.put("fourteen",14);
hm.put("fifteen",15);
hm.put("sixteen",16);
hm.put("seventeen",17);
hm.put("eighteen",18);
hm.put("nineteen",19);
return hm;
}
static HashMap<String, Integer> initDigits2(){
HashMap<String,Integer> hm=new HashMap<String, Integer>();
hm.put("twenty",20);
hm.put("thirty",30);
hm.put("forty",40);
hm.put("fifty",50);
hm.put("sixty",60);
hm.put("seventy",70);
hm.put("eighty",80);
hm.put("ninety",90);
return hm;
}
}
No comments:
Post a Comment