hdu - 1627 Krypton Factor (dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1627

给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的。

按照格式输出。

这个题的关键在于判断字符串是否是符合要求的字符串.

枚举字符串所有子串可能的长度,然后判断即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std;
int n,m;
int a[100];

void dfs(int cur)
{
    for(int i=0;i<m;i++)
    {
        a[cur]=i;
        bool is=0;
        for(int j=1;2*j<=cur+1;j++)  //枚举 所有 的可能长度,
        {
            bool flag=0;
            for(int k=0;k<j;k++) //判断是否符合要求
                if(a[cur-k]!=a[cur-j-k]) {flag=1;break;}
            if(!flag)
            {
                is=1;
                break;
            }
        }
        if(is) continue;
        if(--n==0)
        {
            for(int j=0;j<=cur;j++)
            {
                if(j&&j%64==0) printf("\n");
                else if(j&&j%4==0) printf(" ");
                printf("%c",a[j]+‘A‘);
            }
            printf("\n");
            printf("%d\n",cur+1);
        }
        dfs(cur+1);
        if(n==0) return;
    }
}
int main()
{
   //freopen("a.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        dfs(0);
    }
    return 0;
}
时间: 2024-10-11 23:06:13

hdu - 1627 Krypton Factor (dfs)的相关文章

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 (DFS &amp;&amp; 回溯)

题意 : 如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的 串".例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCBA都是困难的串.程序从输入中读取多行数据,每行包括两个整数n和L(即按此顺序给出),其中n > 0,L的范围是1 ≤ L ≤ 26.根据这些输入,程序要按照字母表升序打印出第n个"hard"字串(由字母表中的前L个字母构成),并在接下来的一行打

[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 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 2710 Max Factor(找最大素数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unawa