[牛客tracker] 2025.12.03 游游的字符重排

题目链接

题目解法:
先将字符串按字典从小到大排序,之后遍历判断前一个字符和后一个字符是否相等,之后通过 next_permutation 函数生成下一个全排列

本题收获:学习到了新函数 next_permutation
可以通过这篇文章对该函数进行学习
截取:
next_permutaiton(start,end)
prev_permutation(start,end)

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    sort(s.begin(),s.end());
    int ans = 0;
    do {
        bool b = true;
        for(int i = 1;i < s.size();i++){
            if(s[i] == s[i - 1]){
                b = false;
                break;
            }
        }
        if(b){
            ans++;
        }
    }while (next_permutation(s.begin(), s.end()));
    cout << ans;
    return 0;
}
posted @ 2025-12-04 13:43  awalol  阅读(0)  评论(0)    收藏  举报