943 - Number Format Translator

All about problems in Volume IX. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

943 - Number Format Translator

Postby peterkwan » Sun Oct 25, 2009 4:00 pm

Here is my code. I got WA but I don't know what I have done wrongly. Please give some inputs that my code fails. Thanks.

Code: Select all
#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;

map<string, long long> m;

string& replace_all(string& str,const string& old_val, const string& new_val)
{
  string::size_type pos(0);   
  while((pos=str.find(old_val))!=string::npos)
    str.replace(pos,old_val.length(),new_val);   
  return str;   
}   

void init() {
  m.insert(pair<string,long long>("zero", 0));
  m.insert(pair<string,long long>("um", 1));
  m.insert(pair<string,long long>("dois", 2));
  m.insert(pair<string,long long>("tres", 3));
  m.insert(pair<string,long long>("quatro", 4));
  m.insert(pair<string,long long>("cinco", 5));
  m.insert(pair<string,long long>("seis", 6));
  m.insert(pair<string,long long>("sete", 7));
  m.insert(pair<string,long long>("oito", 8));
  m.insert(pair<string,long long>("nove", 9));

  m.insert(pair<string,long long>("dez", 10));
  m.insert(pair<string,long long>("onze", 11));
  m.insert(pair<string,long long>("doze", 12));
  m.insert(pair<string,long long>("treze", 13));
  m.insert(pair<string,long long>("catorze", 14));
  m.insert(pair<string,long long>("quatorze", 14));
  m.insert(pair<string,long long>("quinze", 15));
  m.insert(pair<string,long long>("dezasseis", 16));
  m.insert(pair<string,long long>("dezesseis", 16));
  m.insert(pair<string,long long>("dezassete", 17));
  m.insert(pair<string,long long>("dezessete", 17));
  m.insert(pair<string,long long>("dezoito", 18));
  m.insert(pair<string,long long>("dezanove", 19));
  m.insert(pair<string,long long>("dezenove", 19));

  m.insert(pair<string,long long>("vinte", 20));
  m.insert(pair<string,long long>("trinta", 30));
  m.insert(pair<string,long long>("quarenta", 40));
  m.insert(pair<string,long long>("cinquenta", 50));
  m.insert(pair<string,long long>("sessenta", 60));
  m.insert(pair<string,long long>("setenta", 70));
  m.insert(pair<string,long long>("oitenta", 80));
  m.insert(pair<string,long long>("noventa", 90));

  m.insert(pair<string,long long>("cem", 100));
  m.insert(pair<string,long long>("cento", 100));
  m.insert(pair<string,long long>("duzentos", 200));
  m.insert(pair<string,long long>("trezentos", 300));
  m.insert(pair<string,long long>("quatrocentos", 400));
  m.insert(pair<string,long long>("quinhentos", 500));
  m.insert(pair<string,long long>("seiscentos", 600));
  m.insert(pair<string,long long>("setecentos", 700));
  m.insert(pair<string,long long>("oitocentos", 800));
  m.insert(pair<string,long long>("novecentos", 900));


  m.insert(pair<string,long long>("mil", 1000));
  m.insert(pair<string,long long>("milhao", 1000000));
  m.insert(pair<string,long long>("milhoes", 1000000));
  m.insert(pair<string,long long>("biliao", (long long) 10000*100000));
  m.insert(pair<string,long long>("bilioes", (long long)10000*100000));
  m.insert(pair<string,long long>("bilhao",(long long)10000*100000));
  m.insert(pair<string,long long>("bilhoes",(long long)10000*100000));
}

main() {
  init();

  int n;
  string str, token;
  long long n1, n2, n3, pn;
  scanf("%d\n", &n);
  printf("%d\n", n);

  for (int i=0; i<n; i++) {
    getline(cin, str);
    str = replace_all(str, ",", " E");
    istringstream iss(str);

    pn = 1; n2 = 0; n3 = 0;
//    cout << str << endl;
    while (getline(iss, token, ' ')) {
      if (token == "e") {
         n2 += pn;
       //  cout << "N2 = " << n2 << "; P = " << pn << endl;
         pn = 1;
      }
      else if (token == "E") {
        n3 += pn;
        pn = 1;
      }
      else {
         n1 = m.find(token)->second;
         if (n1 >= 1000) {
            if (n2 < n1) {
              pn = (pn + n2) * n1;
              n2 = 0;
            }
            else
              pn *= n1;
         //  cout << "N2 = " << n2 << "; P = " << pn << endl;
         }
         else {
           if (pn <= n1)
             pn *= n1;
           else
             pn += n1;
          // cout << "P = " << pn << endl;
         }
      }
    }
    n2 += pn + n3;
    cout << n2 << endl;
  }
 
  return 0;
}
peterkwan
New poster
 
Posts: 16
Joined: Sun Oct 28, 2007 1:54 pm

Re: 943 - Number Format Translator

Postby brianfry713 » Wed Apr 09, 2014 11:15 pm

I have created a dataset for this problem and emailed the admins.
Check input and AC output for hundreds of problems on uDebug!
brianfry713
Guru
 
Posts: 5446
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 943 - Number Format Translator

Postby baodog » Sun Apr 13, 2014 6:52 pm

Could you post some critical I/O? I keep get WA on this one, but don't know why.
thanks!
baodog
Experienced poster
 
Posts: 202
Joined: Wed Jul 04, 2007 6:53 am

Re: 943 - Number Format Translator

Postby brianfry713 » Mon Apr 14, 2014 10:21 pm

Input:
Code: Select all
20
duzentos vinte quatro biliao quatrocentos noventa cinco milhao trezentos cinquenta oito mil oitocentos cinquenta quatro
oitocentos quarenta cinco biliao quatrocentos vinte seis milhao cem noventa quatro mil quatrocentos sete
duzentos quarenta oito biliao cem seis milhao quatrocentos seis mil cinquenta dois
noventa seis biliao duzentos trinta sete milhao seiscentos noventa seis mil oitocentos oitenta tres
quinhentos cinquenta tres biliao oitocentos cinquenta um milhao novecentos quarenta nove mil quinhentos seis
quinhentos setenta cinco biliao setecentos cinquenta um milhao cem cinquenta seis mil quatrocentos setenta tres
seiscentos quatro biliao duzentos nove milhao quinhentos oito mil setecentos tres
cem biliao sessenta quatro milhao quinhentos sessenta seis mil oitocentos cinquenta dois
quatrocentos sessenta sete biliao cem vinte nove milhao cem vinte mil oitocentos vinte nove
vinte um biliao seiscentos oitenta quatro milhao setecentos sessenta dois mil seiscentos noventa nove
duzentos noventa dois biliao novecentos vinte tres milhao quatrocentos trinta seis mil setecentos cinquenta seis
novecentos noventa cinco biliao setecentos vinte milhao quatrocentos trinta oito mil cem quarenta oito
trezentos cinquenta seis biliao novecentos sessenta dois milhao quinhentos quinze mil cem quarenta quatro
quatrocentos cinquenta cinco biliao oitocentos dezanove milhao novecentos quatro mil sessenta oito
oitocentos trinta biliao duzentos noventa milhao novecentos cinquenta um mil setecentos oitenta quatro
cem vinte um biliao duzentos dezanove milhao novecentos setenta dois mil trinta um
oitenta dois biliao quinhentos trinta oito milhao novecentos oitenta cinco mil seiscentos dezassete
duzentos noventa cinco biliao sete milhao cem quinze mil oitocentos sessenta seis
oitocentos dezanove biliao oitocentos dezanove milhao trezentos vinte tres mil duzentos quarenta oito
novecentos oito biliao novecentos quarenta quatro milhao seiscentos cinquenta sete mil duzentos quarenta tres
AC Output:
Code: Select all
20
224495358854
845426194407
248106406052
96237696883
553851949506
575751156473
604209508703
100064566852
467129120829
21684762699
292923436756
995720438148
356962515144
455819904068
830290951784
121219972031
82538985617
295007115866
819819323248
908944657243
I don't speak Portuguese so let me know if you disagree with any of these, or send me your code and I'll take a look.
Check input and AC output for hundreds of problems on uDebug!
brianfry713
Guru
 
Posts: 5446
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to Volume IX

Who is online

Users browsing this forum: No registered users and 1 guest