poj 1256 Anagram—next_permutation的神奇应用
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                poj 1256 Anagram—next_permutation的神奇应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                ? 題意:給你一條字符串,讓你輸出字符串中字符的全排列,輸出的順序要按它給的奇葩的字典序。
? 題解:要輸出全排列,暴力dfs可以過,但要注意題目的字典序以及相同字符的情況。如果用next_permutation()處理可以簡單很多;我是先將字典序"A a B b...Z z"的每個字母賦予一個值,即從1 2 3...52。然后將給的字符串全部轉(zhuǎn)換成對應(yīng)的數(shù)值后,用next_permutation()進(jìn)行全排列(當(dāng)然 題目給的字典序有一定規(guī)律,所以也可以直接給next_permutation()寫個cmp函數(shù)直接處理字符串)。
1 /** 2 * @author Wixson 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <cstring> 7 #include <cmath> 8 #include <algorithm> 9 #include <queue> 10 #include <stack> 11 #include <vector> 12 #include <utility> 13 #include <map> 14 #include <set> 15 const int inf=0x3f3f3f3f; 16 const double PI=acos(-1.0); 17 const double EPS=1e-8; 18 using namespace std; 19 typedef long long ll; 20 typedef pair<int,int> P; 21 22 char str[20]; 23 char book[110]; 24 int a[50]; 25 void init() 26 { 27 for(int i=0;i<52;i++) 28 { 29 if(i%2) book[i]='a'+i/2; 30 else book[i]='A'+i/2; 31 } 32 } 33 int main() 34 { 35 //freopen("input.txt","r",stdin); 36 init(); 37 int t,n; 38 scanf("%d",&t); 39 while(t--) 40 { 41 scanf("%s",str); 42 n=strlen(str); 43 for(int i=0;i<n;i++) 44 { 45 if(str[i]>='A'&&str[i]<='Z') a[i]=(str[i]-'A')*2; 46 else a[i]=(str[i]-'a')*2+1; 47 } 48 // 49 sort(a,a+n); 50 do 51 { 52 for(int i=0;i<n;i++) putchar(book[a[i]]); 53 putchar('\n'); 54 55 }while(next_permutation(a,a+n)); 56 } 57 return 0; 58 }?
轉(zhuǎn)載于:https://www.cnblogs.com/geek1116/p/6408049.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的poj 1256 Anagram—next_permutation的神奇应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 小米MIX Fold 2内外屏成本超40
- 下一篇: Q6靠边站!奥迪Q9效果图流出:史上尺寸
