UVa 10098: Generating Fast
这道题要求按字典序生成字符串的全排列,不可重复(但字符可以重复,且区分大小写)。
基本思路是先对输入的字符串按字典序排序,然后从第一位开始递归,从所有输入的字符中选出一个填充,然后再选第二位......具体实现看代码。
要注意的是最后的输出方式,不小心的话会莫名其妙的WA,详情见代码。
我的解题代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std; char s[15],ss[15];
int N; int cmp(const void *a, const void *b)
{
return *(char *)a-*(char *)b;
} void print_permutation(int cur, int len)
{
if(cur==len) //满足此条件则ss已经填满,输出
{
for(int i=0; i<len; i++) cout << ss[i]; cout << endl; //这里用cout << ss << endl; 提交就WA了,天知道怎么回事 = - =
return ;
}
for(int i=0; i<len; i++) if(!i || s[i]!=s[i-1]) //判断是否与上一个待选字符是相同的,如果相同就跳过
{
int c1=0,c2=0;
for(int j=0; j<len; j++) if(s[j]==s[i]) c1++;
for(int j=0; j<cur; j++) if(ss[j]==s[i]) c2++;
if(c2<c1) //分别对s和ss中s[i]出现的次数计数,只要c2<c1,就还可以使用s[i]放入ss中
{
ss[cur]=s[i];
print_permutation(cur+1,len);
}
}
} int main()
{
cin >> N;
while(N--)
{
cin >> s;
int len = strlen(s);
qsort(s,len,sizeof(char),cmp); //按字典序对s排序
print_permutation(0,strlen(s));
cout << endl;
}
return 0;
}
UVa 10098: Generating Fast的更多相关文章
- UVA 10098 Generating Fast, Sorted Permutation
		// 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ... 
- (组合数学3.1.1.2)UVA 10098 Generating Fast(使用字典序思想产生所有序列)
		/* * UVA_10098.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> # ... 
- UVA - 10098 - Generating Fast   (枚举排列)
		思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ... 
- uva 10098 Generating Fast(全排列)
		还是用的两种方法,递归和STL,递归那个是含有反复元素的全排列,这道题我 没有尝试没有反复元素的排列,由于从题目上并没有发现一定是有反复元素的() 贴代码: <span style=" ... 
- UVA 10098 用字典序思想生成所有排列组合
		题目: Generating permutation has always been an important problem in computer science. In this problem ... 
- uva10098 Generating Fast, Sorted Permutation
		#include"iostream"#include"stdio.h"#include"string.h"#include"alg ... 
- UVA 11925 - Generating Permutations
		题意: 给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列. 分析: 正常冒泡排序的想法.如果前两个数,前面的大于后面的,则换(特例是n,1不能换).否则,就用2的逆操作,把最后的数放前面. ... 
- UVA - 11992:Fast Matrix Operations
		线段树,注意tag优先级 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cs ... 
- UVa 11925 Generating Permutations (构造法)
		题意:给定一个序列,让你从一个升序列变成该序列,并且只有两种操作,操作1:交换前两个元素,操作2:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点, ... 
随机推荐
- js console.log 打印 对像 数组 详解
			console.log是什么东西,其实就是一个打印js数组和对像的函数而已,就像是php的print_r,var_dump.console.log这个函数本身没什么好说的,这篇博客告诉大家怎么去用这个 ... 
- discuz php判断是手机访问还是电脑访问网站
			首先在模块处填入代码: //手机网页跳转 //如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true function is_mobile(){ $regex_match="/(n ... 
- java中ExecutorService接口
			一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ... 
- [Python 3.x 官方文档翻译]The Python Tutorial Python教程
			Python is an easy to learn, powerful programming language. It has efficient high-level data structur ... 
- 《python基础教程》笔记之 其它语句1
			print 相关 print可以打印多个表达式,只要将它们用逗号隔开就好,结果中每个参数之间都会插入一个空格,使用+可以避免空格,如 >>> print 'age:',42age: ... 
- sunJCE or ibmJce,was服务器下使用des的注意点
			最近开发了一个应用,在tomcat下一切ok,到was上有报错. 打开debug日志,没有异常?? 继续调查发现是我们的程序引用了一个sun很久以前的jar.这个jar需要单独打开message日志 ... 
- oschina服务器软件
			服务器软件 74Apache模块 54Nginx扩展模块 13Radius相关 94PaaS 系统 29服务发现/注册和协调 17Docker 扩展 7Docker 映像 83应用服务器 189HTT ... 
- 【转】listView中,checkBox的显示和隐藏
			原文网址:http://www.cnblogs.com/vicma/p/3460500.html 在listView中,每个item都有一个ChexBox,当显示的时候在listView外面设置一个按 ... 
- codeforces284 div1 B:概率dp
			蛋疼的期末..好久没有A题了,,惭愧啊 昨晚打起精神准备做cf 结果竟然忘记注册了..拿学长号看了看题,今早起来补了一道dp 题目大意: 有n首歌,你需要边听边猜 对于第 i 首歌 每听一分钟你猜出它 ... 
- hadoop2.2.0的WordCount程序
			package com.my.hadoop.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf ... 
