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的cnt  每当满足所有条件 进入递归边界的时候 判断一下cnt是否等于n 然后cnt++ 

  初始化为0的原因是 第一次在主函数中调用的时候 执行判断语句 cnt++ == n的时候 cnt就更新为1了,所以是初始化为0而不是1.

2.是判断是否是简单的串的方法

这是一个由前L个字符组成的字符串,所以有一个i<l的for循环和  a[cur]=‘A‘+i;的语句

int ok为1 表示困难串,因为 判断完之后是困难串,才进入递归dfs

之后,我们是判断后缀是否相同,而不是整个字符串拿来判断,那样会做很多重复的工作导致效率下降。

  初始化isequal=1,然后开始判断,一旦发现不等的,就isequal=0然后break

在循环外面 判断 if(isequanl==1)  如果是1  则是简单串 则ok=0;

如果ok==1  则进入递归  就是这样一个逻辑

for(int i=0;i<l;i++)
    {
        a[cur]=‘A‘+i;
        int ok=1;
        for(int j=1;j*2<=cur+1;j++)
        {
            int isequal=1;

            for(int k=0;k<j;k++)
                if(a[cur-k]!=a[cur-j-k])
                    {isequal=0;break;}

            if(isequal==1)
            {
                //如果是简单串 则ok=0;
                ok=0;break;
            }
        }
        if(ok==1 && getans==false) dfs(cur+1);
    }

3.   得到答案之后记得更新getans 为true  终止剩下的dfs 不然会一直输出

 或者  dfs加入返回值,递归搜索过程中如果有一个成功,就直接退出!!!  使用int型的dfs  找到答案后return 0    其余dfs均return 1  

if(ok) if(!dfs(cur+1)) return 0;
 1 #include <cstdio>
 2 int n,l,cnt;
 3 char a[90];
 4 bool getans;
 5 void dfs(int cur)
 6 {
 7     if(cnt++ == n)
 8     {
 9         getans=true;
10         for(int i=0;i<cur;i++)
11         {
12             if(i!=0 && i%64==0)
13                 printf("\n%c",a[i]);
14             else if(i!=0 && i%4==0)
15                 printf(" %c",a[i]);
16             else
17                 printf("%c",a[i]);
18         }
19         printf("\n");
20
21         printf("%d\n",cur);
22     }
23     for(int i=0;i<l;i++)
24     {
25         a[cur]=‘A‘+i;
26         int ok=1;//ok=1表示困难的串
27         for(int j=1;j*2<=cur+1;j++)
28         {
29             int isequal=1;
30
31             for(int k=0;k<j;k++)
32                 if(a[cur-k]!=a[cur-j-k])
33                     {isequal=0;break;}
34
35             if(isequal==1)
36             {
37                 //如果是简单串 则ok=0;
38                 ok=0;break;
39             }
40         }
41         if(ok==1 && getans==false) dfs(cur+1);
42     }
43 }
44 int main()
45 {
46     while(scanf("%d%d",&n,&l)!=EOF)
47     {
48         if(n==0 && l==0) break;
49         getans=false;
50         cnt=0;
51         dfs(0);
52     }
53
54     return 0;
55 }
时间: 2024-08-04 03:27:28

Uva 129 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

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

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个字母构成),并在接下来的一行打

129 - Krypton Factor(dfs回溯)

回溯法的再次利用,体会精妙之处. 多研究,多做题~~ #include<bits/stdc++.h> using namespace std; int n,L,cnt,s[100]; int dfs(int cur) { if(cnt++==n) { int kase=0,ans=0,ens=0; for(int i=0;i<cur;i++){ printf("%c",'A'+s[i]); kase++; ens++; if(kase==4) { kase=0; an

【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

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个满足条件的字符串.试探过程中,当前的串是不包含相邻相同子串的,所以只需要考虑加

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

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