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

贴代码:

<span style="font-family:Courier New;font-size:18px;">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int cmp(const void *a,const void *b)
{
return *(char *)a - *(char *)b;
}
int main()
{
int T;
char a[15];
scanf("%d",&T);
while(T--)
{
scanf("%s",a);
int len = strlen(a);
qsort(a,len,sizeof(a[0]),cmp);
puts(a);
while(next_permutation(a,a+len))
{
puts(a);
}
puts("");
}
return 0;
} </span>

递归:

<span style="font-family:Courier New;font-size:18px;">#include<stdio.h>
#include<string.h>
#include<stdlib.h> int cmp(const void *a,const void *b)
{
return *(char *)a - *(char *)b;
}
void solve(int len,char *a,char *b,int cur)
{
int i,j;
if(cur == len)
{
puts(b);
return ;
}
else
{
for(i=0; i<len; i++)
{
if(!i||(a[i] != a[i-1]))
{
int ans1 = 0, ans2 = 0;
for(j=0; j<len; j++) if(a[i] == a[j]) ans1++;
for(j=0; j<cur; j++) if(b[j]==a[i]) ans2++;
if(ans2 < ans1)
{
b[cur] = a[i];
solve(len, a, b, cur+1);
}
}
}
}
return ;
}
int main()
{
int T;
char a[15];
char b[15];
scanf("%d",&T);
while(T--)
{
memset(a,'\0',sizeof(a));
memset(b,'\0',sizeof(b));
scanf("%s",a);
int len = strlen(a);
qsort(a,len,sizeof(a[0]),cmp);
solve(len,a,b,0);
puts("");
}
return 0;
} </span>

uva 10098 Generating Fast(全排列)的更多相关文章

  1. UVa 10098: Generating Fast

    这道题要求按字典序生成字符串的全排列,不可重复(但字符可以重复,且区分大小写). 基本思路是先对输入的字符串按字典序排序,然后从第一位开始递归,从所有输入的字符中选出一个填充,然后再选第二位..... ...

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

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

  3. UVA 10098 Generating Fast, Sorted Permutation

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

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

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

  5. generating permunation——全排列(算法汇总)

    本文一共提供了4种全排列的方法,包括递归非字典序版本.递归字典序版本.标准库版本和BFS字典序版本,当然BFS非字典序实现相对于BFS字典序版本更加简洁,稍加修改即可. 说明:递归版本基于网上现有代码 ...

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

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

  7. uva10098 Generating Fast, Sorted Permutation

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

  8. UVA 11925 - Generating Permutations

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

  9. UVA - 11992:Fast Matrix Operations

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

随机推荐

  1. azure云中 mount: wrong fs type, bad option, bad superblock on /dev/sdc1

    2016-01-30 mount失败问题解决 [root@mofficedb2 ~]# mount /dev/sdc /dta mount: you must specify the filesyst ...

  2. [ES6] The Iterator Protocol

    The iterator protocol is used to define a standard way that an object produces a sequence of values. ...

  3. 在OEL 5.4 32bit上使用yum install命令遇到的问题

    在OEL 5.4 32bit上使用yum install命令遇到的问题 [root@localhost yum.repos.d]# yum install elfutils-libelf-devel- ...

  4. vue ---- 组件传值之间使用 v-model

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  5. 蓝桥杯训练 2n皇后

    问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一列或同一 ...

  6. HTML5入门:HTML5的文档声明和基本代码

    HTML5的文档声明: HTML5的文档声明,不同于HTML4.0和XHTML,它精简了许多代码,只保留<!DOCTYPE html>开头,必须位于HTML5文档的第一行,它可以用来告诉浏 ...

  7. 戏说Linux商用数据库

    戏说Linux商用数据库 上一篇文章(http://chenguang.blog.51cto.com/350944/277533)我介绍了Linux下几款开源数据库Mysql,MaxDB.Postgr ...

  8. ASP.NET Web.config学习

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  9. storm1.1运行时问题

    java.lang.NoClassDefFoundError: org/apache/curator/shaded/com/google/common/cache/CacheBuilder 程序并没有 ...

  10. 【Codeforces Round #426 (Div. 2) A】The Useless Toy

    [Link]:http://codeforces.com/contest/834/problem/A [Description] [Solution] 开个大小为4的常量字符数组; +n然后余4,-n ...