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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)