uva 10098 生成字典续序列

生成字典续序列。

//
//  main.cpp
//  10098_1
//
//  Created by Fangpin on 15/3/7.
//  Copyright (c) 2015年 FangPin. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[23],ans[23];
void dfs(int deep,int len){
    if(deep==len)
        puts(ans);
    else{
        for(int i=0;i<len;++i){
            if(!i || s[i]!=s[i-1]){
                int c1=0,c2=0;
                for(int j=0;j<deep;++j) if(ans[j]==s[i]) ++c1;
                for(int j=0;j<len;++j) if(s[i]==s[j]) ++c2;
                if(c1<c2){
                    ans[deep]=s[i];
                    dfs(deep+1,len);
                }
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    int n;
    scanf("%d",&n);
    while(n--){
        scanf("%s",s);
        memset(ans,0,sizeof(ans));
        int len=strlen(s);
        sort(s,s+len);
        dfs(0,len);
        printf("\n");
    }
    return 0;
}

也可以使用c++库函数next_permutation(iter.begin(),iter.end())

//
//  main.cpp
//  10098
//
//  Created by Fangpin on 15/3/7.
//  Copyright (c) 2015年 FangPin. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    int n;
    scanf("%d",&n);
    char s[29];
    while(n--){
        scanf("%s",s);
        int len=strlen(s);
        sort(s,s+len);
        printf("%s\n",s);
        while(next_permutation(s,s+len)){
            printf("%s\n",s);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-08-02 04:26:18

uva 10098 生成字典续序列的相关文章

uva 1608 不无聊的序列(附带常用算法设计和优化策略总结)

uva 1608 不无聊的序列(附带常用算法设计和优化策略总结) 紫书上有这样一道题: 如果一个序列的任意连续子序列中都至少有一个只出现一次的元素,则称这个序列时不无聊的.输入一个n个元素的序列,判断它是不是无聊的序列.n<=200000. 首先,在整个序列中找到只出现一次的元素ai.如果不能找到,那它就是无聊的.不然,就可以退出当前循环,递归判断[1, i-1]和[i+1, n]是不是无聊的序列.然而怎么找ai很重要.如果从一头开始找,那么最差情况下的时间复杂度就是O(n^2)的.而如果从两头

UVa 10098 快速生成有序排列

题意:对输入的字符串,按字典序升序输出所有种排列. 思路:这个基础就是之前将到的枚举排列问题,只不过当时是整型,这里是字符型. 注意:一个是输出的时候每组数组后都要输出一个空行(虽然你去复制sample out会发现最后一组数据没空行,但程序里的确是包括最后一组数据后都有空行) 第二是因为这里是字符型,输出的时候直接按字符串输出即可,但是需要在排列数组的最末位加'\0'!还有,在最开始需要对字符串排序一下. Code: #include<stdio.h> #include<string.

Generating Fast UVA 10098

说说: 这道题的其实就是给你一个字符串,然后输出该字符串所含字符能构成的全部的串,并按字典升序输出.解法的话,无非就是递归实现.先将原字符串排序,然后逐一确定每个位置上的字符.为了防止有重复的字符串出现,每个位置上的字符不能与之前相同.具体的解释请参见刘汝佳的<算法竞赛入门经典>P118,生成可重集的排列. 源代码: #include <stdio.h> #include <string.h> #define MAX 10+5 char s[MAX]; char p[M

元组、字典、序列、对象与参考

例一: 元组:不可变,圆括号里面的逗号 和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组,元组通过圆括号中用逗号分割的项目定义.元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变. #!/usr/bin/python # Filename: using_tuple.py zoo = ('wolf', 'elephant', 'penguin') print 'Number of animals in the zoo is', len(zo

字典,序列,集合,引用

#-*- coding:utf-8 -*- #------字典-------- '''字典类似于通过联系人名查找联系人的详细信息,即,把键(名字)和值(详细情况)联系在一起, 键必须唯一,只能使用不可变的对象如字符串来作为字典的键,但是可以把不可变或者可变对象作为 字典的值.基本说就是应该只使用 ''' #-------练习--------- email = {'张三':'[email protected]',          '李四':'[email protected]',        

[MySQL] 分组排序取前N条记录以及生成自动数字序列,类似group by后 limit

前言:         同事的业务场景是,按照cid.author分组,再按照id倒叙,取出前2条记录出来.        oracle里面可以通过row_number() OVER (PARTITION BY cid,author ORDER BY id DESC) 表示根据cid,author分组,在分组内部根据id排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的),而mysql数据库就没有这样的统计函数,需要自己写复杂的sql来实现. 1,录入测试数据 USE csd

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

思路:生成全排列,用next_permutation,注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; char str[20]; int main() { int n; cin >> n; while(n-

python zip 生成字典

#zip的使用 a=(1,2,3) b=(4,5,6) c=zip(a,b) print c  [(1, 4), (2, 5), (3, 6)] #逆向 print zip(*c) [(1, 2, 3), (4, 5, 6)] #字典 import string str_upper = [ i for i in string.uppercase ] int_list = range(len(str_upprt)) n_dict = dict(zip(int_list, str_upprt)) p

UVA - 11584 Partitioning by Palindromes[序列DP]

UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a lis