全排列问题

OpenJudge 1750:全排列

DFS(不是重点)

#include<bits/stdc++.h> 
using namespace std;
string s;
int len;
bool used[10];
char c[10];
void dfs(int x)
{
    if(x==len+1)
    {
        for(int i=1;i<=len;i++)
        cout<<c[i];
        cout<<endl;
        return;
    }
    for(int i=1;i<=len;i++)
    {
        if(!used[i])
        {
            c[x]=s[i-1];    
            used[i]=true;
            dfs(x+1);
            used[i]=false;
        }
    }
}
int main()
{
    cin>>s;
    len=s.size();
    dfs(1);
    return 0;
}

STL大法 orz

next_permutation(同sort) 函数

这是一个求一个排序的下一个排列的函数

参数传递同sort

另外

next_permutation(start,end)

prev_permutation(start,end)(反)

这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后。

算法原理戳这里(好文)

代码_极简

#include<bits/stdc++.h> 
using namespace std;
char c[10];
int main()
{
    cin>>c;
    int len=strlen(c);
    do
    cout<<c<<endl;
    while(next_permutation(c,c+len));
    return 0;
}

 

posted @ 2019-03-12 20:35  octal_zhihao  阅读(149)  评论(0编辑  收藏  举报