結果

問題 No.539 インクリメント
ユーザー PachicobuePachicobue
提出日時 2017-07-01 00:40:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,885 bytes
コンパイル時間 2,025 ms
コンパイル使用メモリ 168,048 KB
実行使用メモリ 6,940 KB
最終ジャッジ日時 2024-04-15 07:44:18
合計ジャッジ時間 2,782 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 64 ms
6,812 KB
testcase_02 AC 48 ms
6,940 KB
testcase_03 AC 48 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

inline int isnum(const char c)
{
    if (c >= '0' and c <= '9') {
        return c - '0';
    } else {
        return -1;
    }
}

inline char nextch(const char c, bool& car)
{
    if (c != '9') {
        car = false;
        return c + 1;
    } else {
        car = true;
        return '0';
    }
}

string inc(const string& str)
{
    string s = str;
    bool car = true;
    for (int i = str.size() - 1; i >= 0; i--) {
        if (not car) {
            break;
        }
        const char next = nextch(s[i], car);
        s[i] = next;
    }
    if (car) {
        s = "1" + s;
    }
    return s;
}


int main()
{
    int T;
    cin >> T;
    string dummy;
    getline(std::cin, dummy);
    for (auto t = 0; t < T; t++) {
        string s;
        getline(std::cin, s);
        int i = s.size() - 1;
        bool flag = true;
        int tail = 0;
        int pos = -1;
        bool trail = false;
        bool contain = false;
        int size = 0;
        string stnum;
        for (; i >= 0 and flag;) {
            if (isnum(s[i]) == -1) {
                i--;
                continue;
            } else {
                contain = true;
                tail = max(tail, i);
                while (i >= 0) {
                    if (isnum(s[i]) != -1) {
                        stnum.push_back(s[i]);
                    } else {
                        pos = i;
                        flag = false;
                        break;
                    }
                    i--;
                }
                reverse(stnum.begin(), stnum.end());
                if (stnum[0] == '0') {
                    trail = true;
                    size = stnum.size();
                }
            }
        }
        if (not contain) {
            cout << s << endl;
            continue;
        }
        string newst = inc(stnum);
        const int prev_size = newst.size();
        if (trail) {
            for (int i = 0; i < size - prev_size; i++) {
                newst = "0" + newst;
            }
        }
        for (int i = 0; i <= pos; i++) {
            cout << s[i];
        }
        cout << newst;
        for (int i = tail + 1; i < s.size(); i++) {
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}
0