ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1002. Phone Numbers

My code pass the tests "in console", but it is a wrong answer :(. c++
Posted by Ramshellcinox 2 Jun 2015 02:14
Hi everyone, I was trying to solve this problem, and I use an algorithm that works on "codeblocks", but doesn't work here. I would like that someone help me. This is the code, what is wrong with it? It's Inefficient? Thanks.

#include<iostream>
#include<vector>
#include<string>

using namespace std;

string convertir(string s){
    string conv="";

    for(int i=0;i<s.size();i++){
        if(s[i]=='i'||s[i]=='j') conv+="1";
        else if(s[i]=='a'||s[i]=='b'||s[i]=='c') conv+="2";
        else if(s[i]=='d'||s[i]=='e'||s[i]=='f') conv+="3";
        else if(s[i]=='g'||s[i]=='h') conv+="4";
        else if(s[i]=='k'||s[i]=='l') conv+="5";
        else if(s[i]=='m'||s[i]=='n') conv+="6";
        else if(s[i]=='p'||s[i]=='r'||s[i]=='s') conv+="7";
        else if(s[i]=='t'||s[i]=='u'||s[i]=='v') conv+="8";
        else if(s[i]=='w'||s[i]=='x'||s[i]=='y') conv+="9";
        else conv+="0";
    }

    return conv;
}

int main(){
    string number,aux;
    vector<string> dictionary;
    vector<string> converted;
    int n;

    while(cin>>number){
        if(number=="-1") break;

        cin>>n;

        dictionary.clear();
        converted.clear();

        for(int i=0;i<n;i++){
            cin>>aux;
            dictionary.push_back(aux);
            converted.push_back(convertir(aux));
        }

        bool max[100];
        for(int i = 1; i<100; i++){
            max[i]=false;
        }
        max[0]=true;
        vector<string> sequence(number.size()+1,"");

        for(int i=1;i<=number.size();i++){
            for(int j=0;j<n;j++){
                if(converted[j].size()<=i && number.substr(i-converted[j].size(),converted[j].size())==converted[j]){
                        max[i]=true && max[i-converted[j].size()];
                        sequence[i]=sequence[i-converted[j].size()]+dictionary[j]+" ";
                }
            }
        }

        if(max[number.size()]==false) cout<<"No solution."<<endl;
        else{
            aux=sequence[number.size()];
            aux.erase(aux.size()-1);
            cout<<aux<<endl;
        }
    }


    return 0;
}