###### tags: `tutorial` `數講統治下的資專筆記們` `STL`
# Kattis Veci 題序翻譯+題解
## 題序
### 題幹
> Your program will be given an integer $X$. Find the smallest number larger than $X$ consisting of the same digits as $X$.
>
給你的程式一個整數 $X$ 。找出比 $X$ 大且和 $X$ 由一樣的數字構成的最小數。
### 輸入
> The first line of input contains the integer $X\ (1≤X≤999999)$. The first digit in $X$ will not be a zero.
第一行會是一個整數 $X\ (1≤X≤999999)$。
$X$ 的第一位不會是 $0$。
### 輸出
> Output the result on a single line. If there is no such number, output $0$.
把結果輸出單獨一行。如果沒有解,輸出 $0$。
想好怎麼解了嗎?
## 題解
這題的解法滿神奇的——把數字當字串處理。
對於每一個輸入 $X$ ,他的解就會是按照字典序接著的下一個。
所以——只要找下一個字典序的排序就好。
好巧不巧的是,剛好有一個 STL 可以直接把一個序列變成下一種排序—— ``` next_permutation``` 。
### 用法?
```cpp=1
int a[3] = {1, 2, 3};
next_permutation(a, a+3); // {1, 3, 2}
next_permutation(a, a+3); // {2, 1, 3}
next_permutation(a, a+3); // {2, 3, 1}
next_permutation(a, a+3); // {3, 1, 2}
// 順帶一題,如果沒有下一種排法了,
// next_permutation 會回傳 false。
```
是不是很像 ```sort``` 的用法呢?
沒錯,他們都是 STL (Standard Template Library) 的成員喔。
### 扣的
```cpp=1
#include<iostream>
// 記得要加入 algorithm
// 這樣才能用 next_permutation 喔~~
#include<algorithm>
using namespace std;
#define OW0 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main(){OW0
string x;
cin >> x;
// 如果可以對輸入的 x 找到下一個排序的話
// x.begin(), x.end() 的意思是說
// 我要執行的這個動作的範圍是
// x 的從頭到尾
if(next_permutation(x.begin(), x.end())){
// 輸出已經被轉換過的 x
cout << x;
} else {
// 不然就輸出 0
cout << 0;
}
return 0;
}
```
醬就<font color = 'lightgreen'> AC </font>囉~~