結果
問題 | No.539 インクリメント |
ユーザー |
|
提出日時 | 2017-07-01 00:40:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 58 ms / 2,000 ms |
コード長 | 2,885 bytes |
コンパイル時間 | 1,394 ms |
コンパイル使用メモリ | 170,772 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-04 23:10:39 |
合計ジャッジ時間 | 2,256 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 3 |
ソースコード
#include <bits/stdc++.h>#define show(x) cout << #x << " = " << x << endlusing 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;}