poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序。
题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况。如果用next_permutation()处理可以简单很多;我是先将字典序"A a B b...Z z"的每个字母赋予一个值,即从1 2 3...52。然后将给的字符串全部转换成对应的数值后,用next_permutation()进行全排列(当然 题目给的字典序有一定规律,所以也可以直接给next_permutation()写个cmp函数直接处理字符串)。
/**
* @author Wixson
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; char str[];
char book[];
int a[];
void init()
{
for(int i=;i<;i++)
{
if(i%) book[i]='a'+i/;
else book[i]='A'+i/;
}
}
int main()
{
//freopen("input.txt","r",stdin);
init();
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
n=strlen(str);
for(int i=;i<n;i++)
{
if(str[i]>='A'&&str[i]<='Z') a[i]=(str[i]-'A')*;
else a[i]=(str[i]-'a')*+;
}
//
sort(a,a+n);
do
{
for(int i=;i<n;i++) putchar(book[a[i]]);
putchar('\n'); }while(next_permutation(a,a+n));
}
return ;
}
poj 1256 Anagram—next_permutation的神奇应用的更多相关文章
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- next_permutation,POJ(1256)
题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<... ...
- POJ 1256:Anagram
Anagram Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18393 Accepted: 7484 Description ...
- poj 1256 按一定顺序输出全排列(next_permutation)
Sample Input 3aAbabcacbaSample Output AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbaba ...
- 1256 Anagram
题目链接: http://poj.org/problem?id=1256 题意: 根据自定义的字典序: 'A'<'a'<'B'<'b'<...<'Z'<'z' 和输 ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- 【字母全排列】 poj 1256
深搜 注意与STL模版的去重函数唯一的区别就是有去重. #include <iostream> #include <cstdio> #include <string. ...
随机推荐
- 软件图标显示不正常【win7企业版】
现象: 原因: 图标缓存没有把该软件图标建立起来 解决: 一. 1.找到 IconCache.db 2.你要把电脑隐藏文件打开不然找不到这个文件的,组织—文件夹及搜索选项——查看——显示隐藏文件.文件 ...
- maven——项目构建和依赖管理工具
apache maven是一个用于项目构建和依赖管理的工具. 添加archetype https://repo1.maven.org/maven2/archetype-catalog.xml 更改本地 ...
- JS——锚点的运用
锚点的两种形式: 1.<a href="#a">点击到锚点</a> 2.window.location.hash = "#a"; 最后都 ...
- RadioButtonList的兩種實現方式
一種是修改ItemTemplate,即ListBoxItem裏面的内容 <ListBox ItemsSource="{Binding}"> <ListBox.It ...
- Discuz! G变量的使用方法
1,G变量的使用方法: 例如:$_G['style'][boardlogo] 风格变量篇 $_G['style'] => Array (官方模板区 cr180整理 $_G['style'][st ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
- SQL一对多取子表最新记录的所有字段(ROW_NUMBER()OVER()函数的应用)
ROW_NUMBER()OVER() 参数1:分组字段 PARTITION BY ..,..,.... 可选 参数2:排序字段 ORDER BY .. DESC 必须 实例: 根据Confir ...
- 一个带关闭按钮的Div窗口,很漂亮
<html><head><title>JS+CSS实现带关闭按钮的DIV弹出窗口</title><script> function lock ...
- SGU180 Inversions(树状数组求逆序数)
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...
- 基础命令history
history 记录历史命令 环境变量: HISTSIZE: 命令历史记录的条数: HISTFILE: 命令历史记录的文件,~/.bash_history: HISTFILESIZE: 命令历史 ...