URI ONLINE JUDGE SOLUTION 1287 - Online Judge

Latest

This is an Online Judge Solution Base Site. We can discuss & Solve any contest solution in Programming.

Friday, April 10, 2020

URI ONLINE JUDGE SOLUTION 1287

Problem Number: 1287
Problem Name: Friendly Int Parser
Author’s Name: By TopCoder* USA
Timelimit: 1
Problem Category: AD-HOC
Problem Source: https://www.urionlinejudge.com.br/judge/en/problems/view/1287

Solution:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>

using namespace std;

bool checkInvalidChar(string s)
{
    int sz = s.length();
   
    for(int i = 0; i < sz; i++)
        if(s[i] < 48 || s[i] > 57)
            return true;
            
    return false;
}

bool onlyZeros(string s)
{
    int sz = s.length();
   
    for(int i = 0; i < sz; i++)
        if(s[i] != 48)
            return false;
   
    return true;
}

string removeLeftZeros(string s)
{
    int sz = s.length();
    string r;
   
    for(int i = 0; i < sz; i++)
    {
        if(s[i] != '0'){
            r = s.substr(i);
            break;
        }
    }
   
    return r;
}

bool overflow(string s)
{
    char c[1024];
    strcpy(c, s.c_str());

    long long int lli;
    lli = atoll(c);

    if(lli > 2147483647) return true;
    else return false;
}

int main(int argc, char const *argv[])
{
    bool c, o, v;
    string str;
   
    while(getline(cin,str))
    {
        std::replace( str.begin(), str.end(), 'o', '0');
        std::replace( str.begin(), str.end(), 'O', '0');
        std::replace( str.begin(), str.end(), 'l', '1');
        str.erase(std::remove(str.begin(), str.end(), ','), str.end());
        str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
      
        c = checkInvalidChar(str);
        o = onlyZeros(str);
        v = overflow(str);

        if(c || v || str.length() < 1) cout << "error" << endl;
        else if (o) cout << "0" << endl;
        else cout << removeLeftZeros(str) << endl;
    }
   
    return 0;
}

No comments:

Post a Comment

Thanks..