全排列
dfs实现:
//#pragma GCC optimize(2) #pragma comment(linker, "/STACK:10240000,10240000") #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<queue> #include<map> #include<set> #include<stack> #include<list> #include<ctime> #include<ctype.h> #include<stdlib.h> #include<bitset> #include<algorithm> #include<assert.h> #include<numeric> //accumulate #define endl "\n" #define fi first #define se second #define forn(i,s,t) for(int i=(s);i<(t);++i) #define mem(a,b) memset(a,b,sizeof(a)) #define rush() int MYTESTNUM;cin>>MYTESTNUM;while(MYTESTNUM--) #define debug(x) printf("%d\n",x) #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define mp make_pair #define pb push_back #define sc(x) scanf("%d",&x) #define sc2(x,y) scanf("%d%d",&x,&y) #define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define pf(x) printf("%d\n",x) #define pf2(x,y) printf("%d %d\n",x,y) #define pf3(x,y,z) printf("%d %d %d\n",x,y,z) #define ll long long #define ull unsigned long long #define dd double #define pfs puts("*****") #define pfk(x) printf("%d ",(x)) #define kpf(x) printf(" %d",(x)) #define pfdd(x) printf("%.5f\n",(x)); #define pfhh printf("\n") using namespace std; const ll P=1e9; ll mul(ll a, ll b){ll ans = 0;for(;b;a=a*2%P,b>>=1) if(b&1) ans=(ans+a)%P;return ans;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} const int maxn=550; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } bool vis[maxn+3]; int ma[maxn+3]; int len=0; string s; void ddfs(int n) { if(len==n){ forn(i,0,n) pfk(ma[i]); pfhh; return; } for(int i=1;i<=n;++i){ if(!vis[i]){ ma[len++]=i; vis[i]=1; ddfs(n); vis[i]=0; ma[--len]=-1; } } } int main() { int n; while(~sc(n) && n) { mem(ma,-1); len=0; ddfs(n); } return 0; }
STL permutation实现:
//#pragma GCC optimize(2) #pragma comment(linker, "/STACK:10240000,10240000") #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<queue> #include<map> #include<set> #include<stack> #include<list> #include<ctime> #include<ctype.h> #include<stdlib.h> #include<bitset> #include<algorithm> #include<assert.h> #include<numeric> //accumulate #define endl "\n" #define fi first #define se second #define forn(i,s,t) for(int i=(s);i<(t);++i) #define mem(a,b) memset(a,b,sizeof(a)) #define rush() int MYTESTNUM;cin>>MYTESTNUM;while(MYTESTNUM--) #define debug(x) printf("%d\n",x) #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define mp make_pair #define pb push_back #define sc(x) scanf("%d",&x) #define sc2(x,y) scanf("%d%d",&x,&y) #define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define pf(x) printf("%d\n",x) #define pf2(x,y) printf("%d %d\n",x,y) #define pf3(x,y,z) printf("%d %d %d\n",x,y,z) #define ll long long #define ull unsigned long long #define dd double #define pfs puts("*****") #define pfk(x) printf("%d ",(x)) #define kpf(x) printf(" %d",(x)) #define pfdd(x) printf("%.5f\n",(x)); #define pfhh printf("\n") using namespace std; const ll P=1e9; ll mul(ll a, ll b){ll ans = 0;for(;b;a=a*2%P,b>>=1) if(b&1) ans=(ans+a)%P;return ans;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} const int maxn=550; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } bool vis[maxn+3]; int ma[maxn+3]; int len=0; string s; int main() { string s; while(cin>>s) { int len=s.length(); do{ cout<<s<<endl; }while(next_permutation(s.begin(),s.end()));//如果是数组则是next_permutation(s,s+len) //prev_permutation(a,a+len); } return 0; }