c_cpp 给定字符串的排列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 给定字符串的排列相关的知识,希望对你有一定的参考价值。

//https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
#include<bits/stdc++.h>
using namespace std;
vector <string> v;

void func (string s, int l, int r) {
    if (l==r)
        v.push_back(s);
    else {
        for (int i=l;i<=r; i++) {
            swap(s[l], s[i]);
            sort(s.begin()+l+1, s.end());
            func(s, l+1, r);
            swap(s[l], s[i]);
        }
    }
}

int main()
 {
	int t;
	cin>>t;
	cin>>ws;
	while (t-->0) {
	    string s;
	    getline (cin,s);
        int n=s.length();
        sort(s.begin(), s.end());
	    func(s, 0, n-1);
	    for (auto a = v.begin(); a!= v.end(); a++)
            cout<< *a << " ";
        cout<< "\n";
        v.clear();
	}
	return 0;
}

以上是关于c_cpp 给定字符串的排列的主要内容,如果未能解决你的问题,请参考以下文章