题目1369:字符串的排列
生活随笔
收集整理的這篇文章主要介紹了
题目1369:字符串的排列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目1369:字符串的排列
時間限制:1 秒
內存限制:32 兆
題目描述:?
輸入一個字符串,按字典序打印出該字符串中字符的所有排列。例如輸入字符串abc,則打印出由字符a,b,c所能排列出來的所有字符串abc,acb,bac,bca,cab和cba。
?
?
?
每個測試案例包括1行。
輸入一個字符串,長度不超過9(可能有字符重復),字符只包括大小寫字母。
?
?
?
對應每組數據,按字典序輸出所有排列。
?
?
?
?
打印全排列的算法,思想只有一點,對于當前的一位上添加最小的數。挺經典長考的面試題,一定要注意這些個算法。
#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 ; }
?
轉載于:https://www.cnblogs.com/liyangtianmen/p/3374622.html
總結
以上是生活随笔為你收集整理的题目1369:字符串的排列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实例应用 自定义页面taglib标签
- 下一篇: 堆和栈、值类型与引用类型、装箱与拆箱