1 条题解

  • 0
    @ 2025-8-24 23:12:44

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar luuia
    菜。

    搬运于2025-08-24 23:12:44,当前版本为作者最后更新于2025-04-10 17:00:14,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    Solution

    模拟题,按照题意模拟即可。具体模拟一下表达式的计算和单个字符的转移就行了,O(n3)O(n^3)O(n4)O(n^4) 都能过。

    要注意一些 corner case,例如单个 00 是合法的,首位为 00 是不合法的。

    Code

    /**
     *    author: luuia
     *    created: 2025.04.10 14:59:02
     **/
    #include <bits/stdc++.h>
    using ll = long long;
    #define For(i, j, k) for (int i = j; i <= k; i++)
    #define Rep(i, j, k) for (int i = j; i >= k; i--)
    #define pb push_back
    using namespace std;
    const int N = 1e6 + 10;
    string s, t;
    ll n;
    ll tu(string s)
    {
        ll o = 0, f = 1;
        if (s[0] == '-')
            f = -1;
        for (auto ch : s)
            if (ch != '-')
                o = o * 10 + ch - 48;
        return o * f;
    }
    bool ck(string s)
    {
        if ((s[0] != '-' && s.size() > 10) || (s[0] == '-' && s.size() > 11))
            return 1;
        if (s != "0" && ((s[0] == '-' && s[1] == '0') || s[0] == '0'))
            return 1;
        return 0;
    }
    bool chk(string s)
    {
        string t;
        vector<string> vec;
        bool f = 1;
        For(i, 1, s.size() - 1) if (!isdigit(s[i]) && !isdigit(s[i - 1])) return 0;
        for (auto ch : s)
        {
            if (ch == '=')
                vec.pb(f ? t : "-" + t), t = "", vec.pb("###"), f = 1;
            else if (ch == '+')
                vec.pb(f ? t : "-" + t), t = "", f = 1;
            else if (ch == '-')
                vec.pb(f ? t : "-" + t), t = "", f = 0;
            else
                t += ch;
        }
        if (t != "")
            vec.pb(f ? t : "-" + t);
        bool fl = 0;
        vector<string> ct1, ct2;
        for (auto x : vec)
        {
            if (ck(x) || x == "")
                return 0;
            if (x == "###")
            {
                fl = 1;
                continue;
            }
            (fl ? ct2 : ct1).pb(x);
        }
        if (!ct1.size() || !ct2.size() || !isdigit(s.back()) || !isdigit(s[0]))
            return 0;
        ll o1 = 0, o2 = 0;
        for (auto x : ct1)
            o1 += tu(x);
        for (auto x : ct2)
            o2 += tu(x);
        return o1 == o2;
    }
    void sol()
    {
        cin >> s, n = s.length();
        if (chk(s))
        {
            cout << "Correct\n";
            return;
        }
        For(i, 0, n - 1)
        {
            if (!isdigit(s[i]))
                continue;
            For(j, 0, n)
            {
                string t, t1, t2;
                if (i == 0)
                    t1 = "", t2 = s.substr(1, n - 1);
                else if (i == n - 1)
                    t1 = s.substr(0, n - 1), t2 = "";
                else
                    t1 = s.substr(0, i), t2 = s.substr(i + 1, n - i - 1);
                t = t1 + t2;
                if (j < n)
                    t.insert(j, 1, s[i]);
                else
                    t += s[i];
                if (chk(t))
                {
                    cout << t << '\n';
                    return;
                }
            }
        }
        cout << "Impossible\n";
    }
    int main()
    {
        // freopen("input.in","r",stdin);
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        ll T = 1;
        while (T--)
            sol();
        return 0;
    }
    
    • 1

    信息

    ID
    11949
    时间
    3000ms
    内存
    1024MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者