結果

問題 No.927 Second Permutation
ユーザー ajiruajiru
提出日時 2020-02-08 03:08:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 647 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 82,836 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-25 08:36:29
合計ジャッジ時間 2,143 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int main(void) {
	string s;
	cin >> s;
	vector<int> num(s.size());
	set<int> st;
	for (int i = 0; i < s.size(); ++i) {
		num[i] = s[i] - '0';
		st.insert(s[i] - '0');
	}
	sort(num.begin(), num.end(), greater<int>());
	bool ok = true;
	if (st.size() == 1) ok = false;
	for (int i = s.size() - 1; i >= 1; --i) {
		if (i != 1 || num[i] != 0) {
			if (num[i - 1] != num[i]) {
				swap(num[i - 1], num[i]);
				break;
			}
		}
		if (i == 1) ok = false;
	}
	if (!ok) cout << -1 << endl;
	else {
		for (auto x : num) cout << x;
		cout << endl;
	}
	return 0;
}
0