hdu1627 Krypton Factor

题目描述:

给定A-Z中的前L个字符进行任意组合;

对于组合得到的字符串中不含有相邻的重复串的为hard串

按字典序求解第n个hard串

解题思路:回溯法即可

#include <cstring>
#include <cstdio>
using namespace std;
int n,l;
char ans[82];

bool check(int len)
{
    int flag=false;
    for(int t=len-1; t>=(len+1)/2; t--)
    {
        int i=t;
        int j=len;
        while(j>t)
        {
            if(ans[i]!=ans[j])
                break;
            i--,j--;
        }
        if(j==t)
        {
            flag=true;
            break;
        }
    }
    return flag;
}
int t;
bool dfs(int len)
{
    if(t==n)
        return true;
    if(len==80)
        return false;
    for(int i=0; i<l; i++)
    {
        ans[len+1]='A'+i;
        if(!check(len+1))
        {
            t++;
            if(dfs(len+1))
                return true;
        }
        ans[len+1]='\0';
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&l)==2&&(n!=0||l!=0))
    {
        memset(ans,0,sizeof(ans));
        t=0;
        dfs(0);
        int len=strlen(ans+1);
        for(int i=1; i<=len; i++)
        {
            printf("%c",ans[i]);
            if(i%64==0&&i<len)
            {
                printf("\n");
                continue;
            }
            if(i%4==0&&i<len)
                printf(" ");
        }
        printf("\n");
        printf("%d\n",len);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-12 02:52:06

hdu1627 Krypton Factor的相关文章

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

Krypton Factor

Krypton Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 181    Accepted Submission(s): 60 Problem Description You have been employed by the organisers of a Super Krypton Factor Contest in

UVA129 Krypton Factor

问题链接:UVA129 Krypton Factor. 问题简述:题目是氪因子.输入正整数n和L,输出由前L个字符组成的.字典顺序第n小的不含相邻重复字串的字符串.不含相邻重复字串的字符串是指,一个字符串中,任意两个相邻的字串都不相等.输出结果时,对于找到的字符串,每4个字符间加入一个空格,每行输出80个字符. 问题分析:回溯法实现.从第1个字符开始试探,每个字符从"A"开始可以是L个字符之一,直到遇见第n个满足条件的字符串.试探过程中,当前的串是不包含相邻相同子串的,所以只需要考虑加

uva129 - Krypton Factor 7.4.3 困难的串

  7.4.3困难的串 学习点:dfs加入返回值,递归搜索过程中如果有一个成功,就直接退出 //7.4.3 困难的串 #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm> using namespace std; int n,L; int cnt; char v[81]; bool judge(int cur) { fo

【Uva 129】Krypton Factor(困难的串)

You have been employed by the organisers of a Super Krypton Factor Contest in which contestantshave very high mental and physical abilities. In one section of the contest the contestants are tested ontheir ability to recall a sequenace of characters

HDU 1627 Krypton Factor

回溯法:避免无用判断,强化回溯代码的实现过程 题目的大意就是以字典序为准,排列字符串,但要保证一个字符串中不包含相邻的重复子串. Problem Description For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are: BB ABCDACABCAB AB

Uva 129 Krypton Factor

0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 for(int i=0;i<cur;i++) { if(i!=0 && i%64==0) printf("\n%c",a[i]); else if(i!=0 && i%4==0) printf(" %c",a[i]); else printf("%c",a[i]); } 1.还有一个是输出 第n小  怎么来控制第n小   利用的是一个初始化为0的cn

UVa 129 Krypton Factor【回溯】

学习的紫书的回溯,理解起来还是好困难的说啊= = 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #includ

UVa129 Krypton Factor (回溯法)

链接:http://acm.hust.edu.cn/vjudge/problem/19665分析:cnt用于统计困难串个数,以串的各个位置为状态DFS,由于要字典序第k小,所以枚举cur位置上的字符时要从小到大,每次DFS考虑当前串的后缀是否有困难串,找到一个困难串则cnt++然后继续递归枚举.回溯,如果找到第k个后打印输出最后一波return. 1 #include <iostream> 2 #include <algorithm> 3 using namespace std;