题目1369:字符串的排列
生活随笔
收集整理的這篇文章主要介紹了
题目1369:字符串的排列
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目1369:字符串的排列
時(shí)間限制:1 秒
內(nèi)存限制:32 兆
題目描述:?
輸入一個(gè)字符串,按字典序打印出該字符串中字符的所有排列。例如輸入字符串a(chǎn)bc,則打印出由字符a,b,c所能排列出來(lái)的所有字符串a(chǎn)bc,acb,bac,bca,cab和cba。
?
?
?
每個(gè)測(cè)試案例包括1行。
輸入一個(gè)字符串,長(zhǎng)度不超過(guò)9(可能有字符重復(fù)),字符只包括大小寫字母。
?
?
?
對(duì)應(yīng)每組數(shù)據(jù),按字典序輸出所有排列。
?
?
?
?
打印全排列的算法,思想只有一點(diǎn),對(duì)于當(dāng)前的一位上添加最小的數(shù)。挺經(jīng)典長(zhǎng)考的面試題,一定要注意這些個(gè)算法。
#include <iostream> #include <stdio.h> #include <queue> #include <stdio.h> #include <string.h> #include <vector> #include <queue> #include <set> #include <algorithm> #include <map> #include <stack> #include <math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std ; typedef long long LL ; map<char ,int>mp ; char ch[10] ; int used[10] ; int now[10] ; int id ,Len; void dfs(int selc){if(selc==Len){for(int i=0;i<selc;i++)putchar(ch[now[i]]) ;puts("") ;}for(int i=1;i<=id;i++){if(used[i]){now[selc]=i ;used[i]-- ;dfs(selc+1) ;used[i]++ ;}} } int main(){char s[10] ;while(scanf("%s",s)!=EOF){mp.clear() ;Len=strlen(s) ;sort(s,s+Len) ;id=0 ;memset(used,0,sizeof(used)) ;for(int i=0;i<Len;i++){if(mp.find(s[i])==mp.end()){id++ ;mp[s[i]] = id ;ch[id] = s[i] ;used[id]++ ;}elseused[mp[s[i]]]++ ;}dfs(0) ;}return 0 ; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/liyangtianmen/p/3374622.html
總結(jié)
以上是生活随笔為你收集整理的题目1369:字符串的排列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实例应用 自定义页面taglib标签
- 下一篇: 堆和栈、值类型与引用类型、装箱与拆箱