BFS

题目:  51Nod   1384 全排序                             点此链接

给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",

输出为:

1123

1132

1213

1231

1312

1321

2113

2131

2311

3112

3121

3211

Input

输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)

Output

输出S所包含的字符组成的所有排列

Input示例

1312

Output示例

1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211

初看此题,就想到16年的蓝桥杯的题目,解题思路基本一致,不一致的地方是      输入的字符串中          存在重复的字符,然后在把整个排序输出。     (在排序中   ,  字典序在前的先输出)

比如    样例1 :

输入:

32

输出:

23

32     

原超时代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
char str[10000010],s[110],temp[110];
bool vis[110];
int len,cnt=0,num[110];
void Solve()
{
    int len1=strlen(str);    
    strcat(str,temp);    
    str[len1+len]='a';
    cout<<temp<<endl;
}
void bfs(int index) 
{
    if (index==len&&!strstr(str,temp)){     //超时的主要原因在这里      strstr(str,temp)    当执行到后文,str足够长,次数过多的时候,遍历时间就会加长许多,因此超时。
        cnt++;
        Solve();
        return ;
    }
    int i,j;
    for (j=0;j<len;j++){
        if (!vis[j]){ 
            temp[index]=s[j];
            vis[j]=true;
            bfs(index+1);
            vis[j]=false;
        }
    }
}
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{
    cin>>s;
    len=strlen(s);
    for (int i=0;i<len;i++){
        num[i]=s[i]-'0';
    }
    sort(num,num+len,cmp);
    for (int i=0;i<len;i++){
        s[i]=num[i]+'0';
    }
    bfs(0);
    return 0;
}

ac代码:

AC思路是:  先将字符串按字典序排序好,然后只要是   在每次排序的情况下,该排序形成的字符串 的字典序应大于上一次排序形成的字符串的字典序,假若不大于,那么该字符串在前排序中就已经存在(这句话是解题核心)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
char s[110],temp[110],str[110];
bool vis[110];
int len,num[110];
void Solve()
{
    cout<<s<<endl;
    strcpy(temp,s);  
}
void bfs(int index) 
{
    if (index==len&&strcmp(s,temp)>0){
        Solve();
        return ;
    }
    int i,j;
    for (j=0;j<len;j++){
        if (!vis[j]){ 
            s[index]=str[j];
            vis[j]=true;
            bfs(index+1);
            vis[j]=false;
        }
    }
}
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{
    cin>>str;
    len=strlen(str);
    sort(str,str+len);
    bfs(0);
    return 0;
}

原文地址:https://www.cnblogs.com/q1204675546/p/9366051.html