zoukankan      html  css  js  c++  java
  • 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('
    ');
    54 
    55         }while(next_permutation(a,a+n));
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    springBoot、SpringCloud 常用注解
    HashMap
    数据库连接池原理
    三次握手《《=====》》四次握手
    服务器
    二维码
    Nginx
    日志记录
    数据库事务/索引/存储引擎/锁
    Java接口
  • 原文地址:https://www.cnblogs.com/geek1116/p/6408049.html
Copyright © 2011-2022 走看看