日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

poj 1256 Anagram—next_permutation的神奇应用

發布時間:2023/12/13 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1256 Anagram—next_permutation的神奇应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 題意:給你一條字符串,讓你輸出字符串中字符的全排列,輸出的順序要按它給的奇葩的字典序。

? 題解:要輸出全排列,暴力dfs可以過,但要注意題目的字典序以及相同字符的情況。如果用next_permutation()處理可以簡單很多;我是先將字典序"A a B b...Z z"的每個字母賦予一個值,即從1 2 3...52。然后將給的字符串全部轉換成對應的數值后,用next_permutation()進行全排列(當然 題目給的字典序有一定規律,所以也可以直接給next_permutation()寫個cmp函數直接處理字符串)。

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 }

?

轉載于:https://www.cnblogs.com/geek1116/p/6408049.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的poj 1256 Anagram—next_permutation的神奇应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。