还是用的两种方法,递归和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. 【福利】微信小程序130个精选Demo合集

    小编最近在开发小程序,也读到了不少优秀的小程序源码,项目中有些需求可以直接从源码里粘贴复制过来,虽然这样做不利于自己独立编写代码,但比较是给公司做项目啊,秉着效率第一的原则,简直没有什么比ctrl+c ...

  2. JS 中 this 与闭包的结合产生的问题

    代码片段一: var name = "The Window"; var object = { name : "My Object", getNameFunc : ...

  3. struts2的字符串参数

    一定要熟记一个东西,一层引号的是变量,两层引号的是字符串 如"蓝"/'蓝'是变量,而" '蓝' "/ ' "蓝" '是字符串 打代码时要警惕 ...

  4. modSecurity规则学习(三)——SecRule

    通用格式 SecRule VARIABLES OPERATOR [TRANSFORMATION_FUNCTIONS, ACTIONS]   阶段phase (1)request headers (2) ...

  5. Razor数组数据

    控制器层 public ActionResult DemoArray() { Product[] array = { new Product {Name = "Kayak", Pr ...

  6. js插件---Amaze UI dialog如何使用

    js插件---Amaze UI dialog如何使用 一.总结 一句话总结:别人给你列出来的参考手册照着用先 1.在哪里去找插件参考资料或者使用手册(一般位置找不到的时候)? github上面啊,非常 ...

  7. POJ 3049 DFS

    思路:暴搜 //By SiriusRen #include <cstdio> #include <iostream> #include <algorithm> us ...

  8. windows安装memcached

    http://www.cnblogs.com/wujuntian/p/4791220.html

  9. pytest使用问题总结

    问题一.AttributeError: module 'pytest' has no attribute 'allure'解决方法:pip3 uninstall pytest-allure-adapt ...

  10. asp.net core系列 39 Razor 介绍与详细示例

    原文:asp.net core系列 39 Razor 介绍与详细示例 一. Razor介绍 在使用ASP.NET Core Web开发时, ASP.NET Core MVC 提供了一个新特性Razor ...