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:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点, ...
随机推荐
- 我自己的style
/** DATE:Time AUTHOR:Zoe TEAM:公司名称 INTRO:cssName **/ @charset "utf-8"; /*通用公共样式 开始*/ /* 清除 ...
- php获取汉字首字母
php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. function getFirstCharter($str){ if(empty($str)){return ...
- Linux C判断日期格式是否合法
Title:Linux C判断日期格式是否合法 --2013-10-11 11:54 #include <string.h> // strlen() , strncpy() #includ ...
- ural 1333 Genie Bomber 2
#include <cstdio> #include <cstring> #include <algorithm> #define maxn 200 using n ...
- RTSP Monitor的总结
项目描述: 一个本地的IP Camera 实时发送RTSP视频流到本机上,视频的帧是H264编码,需要解码并显示到屏幕上.并把每帧视频对应的时间戳转换成日期年月日时分秒打印到每帧的图像上显示. 使用 ...
- Android ListView特别属性用法
由于这两天在做listView的东西,所以整理出来一些比较特别(不常用)的属性,通过设置这样的属性可以做出更加美观的列表 1.stackFromBottom属性,这只该属性之后你做好的列表就会显示你列 ...
- 提升效率的Linux终端快捷操作汇总
很多普通 Linux 桌面用户都对使用终端感到排斥和恐惧,其实它没大家想的那么复杂,很多常见操作都可以直接在终端中进行,如:安装软件.升级系统等. 无论你是新手还是 Linux 终端使用的老鸟,系统极 ...
- [每日一题] 11gOCP 1z0-053 :2013-09-30 ASMCMD.......................................................8
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12206095 正确答案:BCD 为了使ASM文件管理更简单,Oracle提供了一个命令实用 ...
- Spring中Ioc容器的注入方式
1 通过setter方法注入 bean类: package com.test; public class UserServiceImplement implements IUserService { ...
- writev/readv
```cpp#include <sys/uio.h>ssize_t readv(int fd, const struct iovec *iov, int iovcnt);ssize_t w ...