文章

39

粉丝

0

获赞

87

访问

7.2k

头像
全排列 题解:
P1185 中国矿业大学/北京大学机考题
发布于2026年3月19日 14:59
阅读数 32

if和for对于递归全排列的选择

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

void dfs(string& s, vector<bool>& used, string& path) {
    if (path.size() == s.size()) {
        cout << path << endl;
        return;
    }

    for (int i = 0; i < s.size(); i++) {//为了全排列选择不能用if因为if无法从开头遍历出没选的再选
        if (used[i]) continue;//一般都是有标记数组判断选没选

        

        used[i] = true;
        path.push_back(s[i]);
        dfs(s, used, path);
        path.pop_back();
        used[i] = false;
    }
}

int main() {
    string s;
    cin >> s;
    sort(s.begin(), s.end()); // 排序是为了去重判断
    vector&l...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发