CSES – Creating Strings

遞迴、排列枚舉

#include<bits/stdc++.h>
#define int long long
#define double long double
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
//declare
string s,tmp;
int n,cnt['z'+1] = {0},times=0;
vector<string>ans;
//
void solve(int x,char put){
    if(x == n){
        ans.push_back(tmp);
        times++;
        return;
    }
    for(int i='a'; i<='z'; i++){
        if(cnt[i]){
            cnt[i]--;
            tmp += i;
            solve(x+1,i);
            tmp.pop_back();
            cnt[i]++;
        }
    }
}
signed main(){
    fastio;
    cin>>s; n = s.size();
    for(char c:s) cnt[c]++;
    solve(0,s[0]);
    cout<<times<<"\n";
    for(string t:ans) cout<<t<<"\n";
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *