結果

問題 No.539 インクリメント
ユーザー tkmst201tkmst201
提出日時 2017-06-30 22:50:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,615 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 162,020 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 06:50:11
合計ジャッジ時間 2,233 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

string sum(string a, string b) {
	if (a.size() > b.size()) swap(a, b);
	
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	
	string res = "";
	int c = 0;
	REP(i, a.size()) {
		c += a[i] - '0' + b[i] - '0';
		res.push_back(c % 10 + '0');
		c /= 10;
	}
	FOR(i, a.size(), b.size()) {
		c += b[i] - '0';
		res.push_back(c % 10 + '0');
		c /= 10;
	}
	if (c > 0) res.push_back(c + '0');
	
	reverse(res.begin(), res.end());
	
	return res;
}

int main() {
	int T;
	cin >> T; cin.ignore();
	while (T--) {
		string S;
		getline(cin, S);
		
		int p[10];
		memset(p, -1, sizeof(p));
		
		REP(i, 10) {
			if (S.rfind(i + '0') == string::npos) continue;
			p[i] = S.rfind(i + '0');
		}
		
		int r = -1;
		REP(i, 10) chmax(r, p[i]);
		
		if (r == -1) {
			cout << S << endl;
			continue;
		}
		
		int l = r;
		while (l >= 0 && isdigit(S[l])) l--;
		l++;
		
		string ans = S.substr(0, l);
		
		string num = sum(S.substr(l, r - l + 1), "1");
		
		ans += num;
		ans += S.substr(r + 1, S.size());
		
		cout << ans << endl;
	}
	return 0;
}
0