所有的排列,但是要不重复

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<sstream>
  4. #include<queue>
  5. #include<map>
  6. #include<memory.h>
  7. #include <math.h>
  8. #include<time.h>
  9. #include <stdlib.h>
  10. #include <algorithm>
  11. using namespace std;
  12. #define N 12
  13. int vis[N];
  14. char a[N];
  15. char b[N];
  16. int length;
  17. int n;
  18. int final = 0;
  19. void dfs(int cur)
  20. {
  21. if(cur == length)
  22. {
  23. for(int i = 0; i < cur; i++)
  24. cout << b[i];
  25. cout << endl;
  26. return;
  27. }
  28. for(int i = 0; i < length; i++)
  29. {
  30. if(!i || a[i] != a[i - 1])
  31. {
  32. int c1 = 0, c2 = 0;
  33. for(int j = 0; j < cur; j++)
  34. if(b[j] == a[i])
  35. c1++;
  36.  
  37. for(int j = 0; j < length; j++)
  38. if(a[j] == a[i])
  39. c2++;
  40. if(c1 < c2)
  41. {
  42. b[cur] = a[i];
  43. dfs(cur + 1);
  44. }
  45. }
  46.  
  47. }
  48. }
  49. void sort()
  50. {
  51. for(int i = 0; i < length; i++)
  52. {
  53. for(int j = 1; j < length - i; j++)
  54. {
  55. if(a[j] < a[j - 1])
  56. {
  57. char cc = a[j];
  58. a[j] = a[j - 1];
  59. a[j - 1] = cc;
  60. }
  61. }
  62. }
  63.  
  64. }
  65. int main(const int argc, char** argv)
  66. {
  67. freopen("d:\\1.txt", "r", stdin);
  68. cin >> n;
  69. while (n--)
  70. {
  71. memset(a, 0, sizeof(a));
  72. scanf("%s", a);
  73. length = strlen(a);
  74. sort();
  75. dfs(0);
  76. //if(n != 0)
  77. cout << endl;
  78. }
  79. return 0;
  80. }

  

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<sstream>
  4. #include<queue>
  5. #include<map>
  6. #include<memory.h>
  7. #include <math.h>
  8. #include<time.h>
  9. #include <stdlib.h>
  10. #include <algorithm>
  11. using namespace std;
  12. #define N 12
  13. int vis[N];
  14. char a[N];
  15. char b[N];
  16. int length;
  17. int n;
  18. int final = 0;
  19. void dfs(int cur)
  20. {
  21. if(cur == length)
  22. {
  23. for(int i = 0; i < cur; i++)
  24. cout << b[i];
  25. cout << endl;
  26. return;
  27. }
  28. for(int i = 0; i < length; i++)
  29. {
  30. if(!i || a[i] != a[i - 1])
  31. {
  32. int c1 = 0, c2 = 0;
  33. for(int j = 0; j < cur; j++)
  34. if(b[j] == a[i])
  35. c1++;
  36.  
  37. for(int j = 0; j < length; j++)
  38. if(a[j] == a[i])
  39. c2++;
  40. if(c1 < c2)
  41. {
  42. b[cur] = a[i];
  43. dfs(cur + 1);
  44. }
  45. }
  46.  
  47. }
  48. }
  49. void sort()
  50. {
  51. for(int i = 0; i < length; i++)
  52. {
  53. for(int j = 1; j < length - i; j++)
  54. {
  55. if(a[j] < a[j - 1])
  56. {
  57. char cc = a[j];
  58. a[j] = a[j - 1];
  59. a[j - 1] = cc;
  60. }
  61. }
  62. }
  63.  
  64. }
  65. int main(const int argc, char** argv)
  66. {
  67. freopen("d:\\1.txt", "r", stdin);
  68. cin >> n;
  69. while (n--)
  70. {
  71. memset(a, 0, sizeof(a));
  72. scanf("%s", a);
  73. length = strlen(a);
  74. sort();
  75. do
  76. {
  77. cout << a << endl;
  78. } while (next_permutation(a, a + length));
  79. cout << endl;
  80. }
  81. return 0;
  82. }

  想看库函数源码了。。。。。

uva-10098的更多相关文章

  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

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

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

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

  5. uva 10098 Generating Fast(全排列)

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

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

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

  7. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  8. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  10. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

随机推荐

  1. [LeetCode&Python] Problem 821. Shortest Distance to a Character

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  2. 带CookieContainer进行post

    1.获取CookieContainer ——用户登录 CookieContainer cookie = new CookieContainer(); UserLoginPost("post地 ...

  3. BC32(hdu5182~5185)

    恩……又是一个悲伤的故事,然后BC做出来一题,因为自己傻逼,可能紧张,也可能是其他,反正没看全题目就敲,敲完WA,WA完改,改完WA,没了……大概五十几分钟WA了五法,然后问了才知道没看全,就这样,后 ...

  4. 租酥雨的NOIP2018赛前日记

    租酥雨的NOIP2018赛前日记 离\(\mbox{NOIP2018}\)只剩下不到一个月的时间辣! 想想自己再过一个月就要退役了,觉得有必要把这段时间的一些计划与安排记录下来. 就从国庆收假开始吧. ...

  5. Hadoop storm大数据分析 知识体系结构

    最近工作工作有用到hadoop 和storm,最近看到一个网站上例句的hadoop 和storm的知识体系.所以列出来供大家了解和学习.来自哪个网站就不写了以免以为我做广告额. 目录结构知识点还是挺全 ...

  6. pipelinedb Continuous transforms 操作

    Continuous transforms 可以进行数据的转换,数据是不进行存储,主要是可以加入到其他的stream pipeline 中,或者写到其他外部 存储中,和存储过程结合使用,当前默认内置一 ...

  7. gitlab-ce-omnibus社区版的备份、还原及升级

    gitlab-ce-omnibus社区版的备份和还原,可以使用gitlab自带工具,gitlab-rake来完成,详见下面例子 将旧gitlab服务器备份,并还原至新gitlab服务器 ,这两台git ...

  8. 什么是Map-Reduce

    Map-Reduce本身并不是算法:而是一种处理模式:因为在大数据分布式这种场景下,处理数据运算和单机版不同:需要协同多台机器,并行计算:于是有了map-reduce这种模式,map阶段是数据处理,在 ...

  9. Centos 6.5 yum 安装Apache软件

    首先在系统上面查询一下是否已经安装了apache 软件[Apache软件在linux系统里的名字是httpd] rpm    -qa    httpd 如果有返回的信息,则会显示已经安装的软件.如果没 ...

  10. javascript 变量定义

    一.javascript中,变量定义的位置与写在哪个<script></script>标签对内无关,只分前后顺序,写在前面的后面就能够访问,写在后面的前面会提示“未定义”. 例 ...