这道题要求按字典序生成字符串的全排列,不可重复(但字符可以重复,且区分大小写)。

基本思路是先对输入的字符串按字典序排序,然后从第一位开始递归,从所有输入的字符中选出一个填充,然后再选第二位......具体实现看代码。

要注意的是最后的输出方式,不小心的话会莫名其妙的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的更多相关文章

  1. UVA 10098 Generating Fast, Sorted Permutation

    // 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...

  2. (组合数学3.1.1.2)UVA 10098 Generating Fast(使用字典序思想产生所有序列)

    /* * UVA_10098.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> # ...

  3. UVA - 10098 - Generating Fast (枚举排列)

    思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...

  4. uva 10098 Generating Fast(全排列)

    还是用的两种方法,递归和STL,递归那个是含有反复元素的全排列,这道题我 没有尝试没有反复元素的排列,由于从题目上并没有发现一定是有反复元素的() 贴代码: <span style=" ...

  5. UVA 10098 用字典序思想生成所有排列组合

    题目: Generating permutation has always been an important problem in computer science. In this problem ...

  6. uva10098 Generating Fast, Sorted Permutation

    #include"iostream"#include"stdio.h"#include"string.h"#include"alg ...

  7. UVA 11925 - Generating Permutations

    题意: 给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列. 分析: 正常冒泡排序的想法.如果前两个数,前面的大于后面的,则换(特例是n,1不能换).否则,就用2的逆操作,把最后的数放前面. ...

  8. UVA - 11992:Fast Matrix Operations

    线段树,注意tag优先级 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cs ...

  9. UVa 11925 Generating Permutations (构造法)

    题意:给定一个序列,让你从一个升序列变成该序列,并且只有两种操作,操作1:交换前两个元素,操作2:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点, ...

随机推荐

  1. Python自动化运维之10、模块之json、pickle、XML、PyYAML、configparser、shutil

    序列化 Python中用于序列化的两个模块 json     用于[字符串]和 [python基本数据类型] 间进行转换 pickle   用于[python特有的类型] 和 [python基本数据类 ...

  2. Solr Dataimport配置

    参考资料: https://cwiki.apache.org/confluence/display/solr/Uploading+Structured+Data+Store+Data+with+the ...

  3. Swift—Core Foundation框架-备

    Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Swift中调用这种C语言风格的API比较麻烦,但是在OS X和iOS开发 ...

  4. Collection Views and Building Custom Layouts-备

    UICollectionView的结构回顾 首先回顾一下Collection View的构成,我们能看到的有三个部分: Cells Supplementary Views 追加视图 (类似Header ...

  5. 『安全工具』Nmap 强悍的端口扫描工具

    作为时下流行的端口扫描工具,Nmap有因其扫描的隐密性有“端口扫描之王”之称 上图是黑客帝国(The Matrix)中崔妮蒂用Nmap入侵核发电站的能源管理系统 0x 01 Nmap介绍 Nmap是一 ...

  6. Printing Architecture

    Printing Architecture http://www.codeproject.com/Articles/8916/Printing-Architecture     This articl ...

  7. FE: CSS固定图片显示大小及GitHub Pages在线演示

    CSS固定图片显示大小 分析 假设图片区域的大小固定为250×300px,那么我们可以写出如下的样式 .picture-area { width: 250px; height: 300px; marg ...

  8. 客户端把rsyslog重启,就会发送全部日志 --待研究

    客户端: uat-web02:/var/log/nginx# echo "scan-cccc21231">>scan.log uat-web02:/var/log/ng ...

  9. 11g v$session定位客户端IP

    11g v$session 新增PORT 字段 用于描述客户端的端口号 客户机从10.5.129.180 访问10.5.128.28 [oracle@cpool ~]$ netstat -na | g ...

  10. 【HDOJ】3184 All Can Do

    简单数学题. #include <cstdio> #include <cstring> #include <cstdlib> int main() { int t; ...