poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256
思路分析:该题为含有重复元素的全排列问题;由于题目中字符长度较小,采用暴力法解决。
代码如下:
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ;
char P[MAX_N], A[MAX_N]; char * SortAlp(char P[], int n)
{
int Low[MAX_N], Upper[MAX_N];
int LowLen, UpperLen; LowLen = UpperLen = ;
for (int i = ; i < n; ++ i)
{
if ('A' <= P[i] && P[i] <= 'Z')
Upper[UpperLen++] = P[i];
else
Low[LowLen++] = P[i];
}
sort(Low, Low + LowLen);
sort(Upper, Upper + UpperLen); int Index_L, Index_U;
Index_L = Index_U = ;
for (int j = ; j < n; ++j)
{
if (Upper[Index_U] - 'A' + 'a' <= Low[Index_L]
&& Index_U < UpperLen)
P[j] = Upper[Index_U++];
else
P[j] = Low[Index_L++];
}
return P;
} void PrintPermutation(int n, char P[], char A[], int cur)
{
int i, j; if (cur == n)
{
for (i = ; i < n; ++i)
printf("%c", A[i]);
printf("\n");
}
else
{
for (i = ; i < n; ++i)
{
if (!i || P[i] != P[i-])
{
int c1 = , c2 = ; for (j = ; j < cur; ++j)
if (A[j] == P[i]) c1++;
for (j = ; j < n; ++j)
if (P[i] == P[j]) c2++; if (c1 < c2)
{
A[cur] = P[i];
PrintPermutation(n, P, A, cur + );
}
}
}
}
} int main()
{
int n;
char P[MAX_N]; cin >> n;
for (int i = ; i < n; ++i)
{
cin >> P;
SortAlp(P, strlen(P));
PrintPermutation(strlen(P), P, A, );
}
return ;
}
poj 1256 Anagram(dfs)的更多相关文章
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- POJ 1256:Anagram
Anagram Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18393 Accepted: 7484 Description ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- poj 1816 (Trie + dfs)
题目链接:http://poj.org/problem?id=1816 思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字 ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
随机推荐
- des 加密 iOS
转载请注明原文出处 生成同名的Des.h文件和Des.m文件后,拷贝下方的代码可直接使用.注意在Des.m文件中将key值修改为项目所需的key.---> #define DesKey 过程很 ...
- C# 课堂总结3-语句
一.顺序语句 二.条件,分支语句 1.if语句 关键是能够熟练运用 if的嵌套.要考虑好所有的情况. 如果说 条件是两种+情况相互对应的,那么就可以只用 if 与else .但必须要想好 每个else ...
- 枚举与define的区别
1.枚举enum的用途浅例 写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等. ...
- 在Mac pro上如何配置adb命令?
在Mac pro上如何将Android SDK的adb命令添加到环境变量中,这里将进行说明! 方法/步骤 1 启动终端,可以在Spotlight中搜索“终端” 2 进入当前用户的HOME目录,命令如下 ...
- codeforces 620F. Xors on Segments
题目链接 定义一种操作f(u, v) = u^u+1^.......^v. (u<=v), 给n个数, q个询问, 每个询问给出一个区间[l, r], 求这个区间里的f(a[i], a[j]) ...
- [LeetCode]题解(python):134-Gas Station
题目来源: https://leetcode.com/problems/gas-station/ 题意分析: 在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油.现在有一辆无限容量的车,它从 ...
- 10min系列之二日志可视化进阶
10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...
- Sublime 配置&插件推荐
sublime 配置&插件推荐 Sublime编辑器的新鲜特性同时选中多个 先选中一个 再Command + D Command + P @搜索函数 #搜索关键字迷你地图 安装package ...
- Qt在Linux环境下应用程序字体模糊的解决方法(先改成使用默认字体,然后使用qtconfig配置)
这两天一直在用Qt实现一个跨平台的软件.软件之前在Windows上编写的,后来放到里Ubuntu 10.10下编译.程序运行时遇到一个很棘手的问题,界面文本非常模糊.后来在网上查阅了好几天的资料,经历 ...
- 最佳实践:Windows Azure 网站 (WAWS)
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Sunitha Muthukrishna 撰写. Windows Azure 网站 (WAWS) 允许您在 Windows ...