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 ...
随机推荐
- POJ1363:Rails
Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...
- ios7高级
1.实现控制器和视图. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 ...
- iOS网络开发-AFNetworking请求asp.net WebService
看到园子有位朋友需要使用AFN框架请求 WebService,所以就整理了一下,demo下载链接在底部 编写WebService可以看这篇博客 http://www.cnblogs.com/linmi ...
- POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)
题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...
- 经典union的使用
一个用户下广告位 某一天有收入和支出 有支出不一定有收入 有收入不一定有支出 下例为按用户查询 sanhao 下的信息 支出如下: 收入如下: 按天进行查询,例如查询: 得到结果如下: 使用一 ...
- 转: Nodejs概述
目录 简介 安装与更新 版本管理工具nvm 基本用法 REPL环境 异步操作 全局对象和全局变量 模块化结构 概述 核心模块 自定义模块 fs模块 Stream模式 http模块 实例:搭建一个HTT ...
- Qt在Linux环境下应用程序字体模糊的解决方法(先改成使用默认字体,然后使用qtconfig配置)
这两天一直在用Qt实现一个跨平台的软件.软件之前在Windows上编写的,后来放到里Ubuntu 10.10下编译.程序运行时遇到一个很棘手的问题,界面文本非常模糊.后来在网上查阅了好几天的资料,经历 ...
- android studio recent projects
android studio正常的删除项目的方法是可以点击右键,选择project structure,进入到界面时,选择你要删除的工程,点击减号,接着就可以右键工程有一个delete. 另外一种方法 ...
- sqlite性能简单測试
主要測试sqlite在大数据量下的插入及查询性能: 測试环境:Centos6.4 1G内存 单核 数据量 大小 索引字段检索(耗时) 非索引字段检索(耗时) 总插入时间 10W 19M 0.001 ...
- openstack之Glance
一.Glance简介.基本概念: Glance是openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等操作. Glance提供restful API可以查询虚拟机镜像的m ...