Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I stuck at a problem SPOJ.com - Problem ABSYS[^]

I executed my program on all the test cases ,I am getting correct answer.

But still I am getting as "wrong answer" on spoj

What I have tried:

Breaking stream into strings storing them into vectors ,so we can say that our vector will have 5 strings (numstring,+,numstring,=,numstring) and then we will inspect each string whenever we found m in the string ,we get to know that string at this position is variable that need to be calculated with the help of other digits strings

And we are also certain about that string with digits can only be at 0,2,4 indices in the vectors other two indices will have + and =


Test cases:

Input:

3

23 + 47 = machula

3247 + 5machula2 = 3749

machula13 + 75425 = 77038
Output:

23 + 47 = 70

3247 + 502 = 3749

1613 + 75425 = 77038

What I have tried:

#include<iostream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
int main() {
    int t;
    (cin >> t).get();
    while (t--) {
        string str;

        getline(cin, str);
        stringstream split(str);
        string each;
        vector <string> tokens;
        while (getline(split, each, ' ')) {
            ///scan each part of stream;
            tokens.push_back(each);

        }
        int ind = -1;
        for (int i = 0; i < tokens.size(); i++) {
            string temp;
            temp = tokens[i];
            for (int j = 0; j < temp.length(); j++) {
                if (temp[j] == 'm') {
                    ind = i;
                    break;
                }

            }
            if (ind != -1)
                break;
        }
        int i1, i2;
        string str1;
        if (ind == 0) {
            i1 = stoi(tokens[2]);
            i2 = stoi(tokens[4]);
            int result = i2 - i1;
            cout << result << " + " << i1 << " = " << i2 << "\n";
            //break;
        }
        else if (ind == 2) {
            i1 = stoi(tokens[0]);
            i2 = stoi(tokens[4]);
            int result;
            result = i2 - i1;
            cout << i1 << " + " << result << " = " << i2 << "\n";
            //break;
        }
        else if (ind == 4) {
            i1 = stoi(tokens[0]);
            i2 = stoi(tokens[2]);
            int result = i1 + i2;
            cout << i1 << " + " << i2 << " = " << result << "\n";
            //break;
        }
        tokens.clear();
        str.erase();
    }


}
Posted
Updated 28-May-20 16:50pm
Comments
Richard MacCutchan 19-Jan-19 4:39am    
You should be asking this question at spoj.com.

I wouldn't be posting the entire code here, but I can surely help.

This is how I would have had implemented the algorithm.

First, take input in a string STR.
Then break the string into 3 parts = NO1, NO2, ANS
Now check in each string, if it contains the string "machula"
Parse the string with "machula" as integer as solve the equation further.

For eg:
Taking sample test case 1:
Quote:
23 + 47 = machula


Then, split the input into 3 parts:
NO1 = 23
NO2 = 47
ANS = machula.

Checking each string for "machula" eliminates NO1 and NO2.

Let INT1, INT2, and INT3 be the Integer values of the strings.

Then:
INT1 = 23
INT2 = 47

And we have to find INT3.

Note: You can refer to Converting Strings to Numbers in C/C++ - GeeksforGeeks[^] to convert a string into an integer.
 
Share this answer
 
Quote:
But still I am getting as "wrong answer" on spoj

Some site give logs of tests, any idea of the failing test ?
Quote:
Breaking stream into strings storing them into vectors ,so we can say that our vector will have 5 strings (numstring,+,numstring,=,numstring)

Where have you seen that there is a space between each part of the equations ?
I have not seen it written in problem description.
Your code is not checking the number of parts after split.

Instead of splitting at spaces, I would use Regex to match '+' and '=' and exclude spaces.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900