首页 > 解决方案 > spoj 上的防污系统

问题描述

我遇到了一个问题Anti Blot

我在所有测试用例上执行了我的程序,我得到了正确的答案。

但我仍然在 spoj 上得到“错误答案”

我试过的:

将流分解成字符串,将它们存储到向量中,所以我们可以说我们的向量将有 5 个字符串(numstring,+,numstring,=,numstring),然后我们将检查每个字符串,只要我们m在字符串中找到,我们就会知道此位置的字符串是变量,需要借助其他数字字符串来计算

而且我们也确定带有数字的字符串只能位于0,2,4其他两个索引将具有的向量中的索引处+ and =

测试用例:

输入:

3

23 + 47 = 马丘拉

3247 + 5machula2 = 3749

马丘拉13 + 75425 = 77038

输出:

23 + 47 = 70

3247 + 502 = 3749

1613 + 75425 = 77038

#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();
    }


}

标签: c++stringalgorithm

解决方案


我知道这很烦人,您的解决方案没有逻辑错误,但您应该更仔细地阅读问题描述。它说:

每个测试用例前面都有一个空行。

所以输入是:

3

23 + 47 = machula

3247 + 5machula2 = 3749

machula13 + 75425 = 77038

注意行之间的空白行。只需将您的代码修复为:

// Blablabla before getline
getline(cin, str);
getline(cin, str);
stringstream split(str);
// The rest of your code

这应该可以解决问题。


推荐阅读