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. ...
随机推荐
- 实现div毛玻璃背景
毛玻璃效果 ios里毛玻璃效果的使用非常多,本文介绍一个实现div毛玻璃背景的方法 CSS3 Filter CSS3的Filter主要用在图像的特效处理上,默认值为none,还有以下备选项: 1. ...
- android学习之路资料集合
版权声明:本文为 stormzhang 原创文章,可以随意转载,但必须在明确位置注明出处!!! 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回答如何自学android编程, 就借这个机会在 ...
- [Windows Server 2003] 还原SQL Server数据库
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:SQL Ser ...
- 遍历select搜索结果,只取数字标key值,防止重复
//遍历select搜索结果,只取数字标key值,防止重复 foreach ($row as $key => $value) { if (is_int($key)) { echo $value; ...
- 9-4前端vue面试的问题
就没有什么顺序了,肖师傅的一些提问: 1- 配置文件中proxyTable的作用 2-@import '~styles/mixins.styl' ~的作用 3-vue模拟的本地中访问地址的url带有 ...
- Resetting the SMC & PRAM
Resetting the SMC & PRAM on portables with a battery you should not remove on your own 1. Shut d ...
- C# SetWindowsHookEx
[DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookPr ...
- perf-perf stat用户层代码分析
perf_event 源码分析 前言 简单来说,perf是一种性能监测工具,它首先对通用处理器提供的performance counter进行编程,设定计数器阈值和事件,然后性能计数器就会在设定事件发 ...
- python 易忘操作整理
>>> l=[1,2,3,4,5] >>> s='$'.join(str(i) for i in l) >>> print(s,end=" ...
- PAT 1103 Integer Factorization
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...