Krypton Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 392    Accepted Submission(s): 174

Problem Description
You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physical abilities. In one section of the contest the contestants are tested on their ability to recall a sequenace of characters which has been read to them by the Quiz Master. Many of the contestants are very good at recognising patterns. Therefore, in order to add some difficulty to this test, the organisers have decided that sequences containing certain types of repeated subsequences should not be used. However, they do not wish to remove all subsequences that are repeated, since in that case no single character could be repeated. This in itself would make the problem too easy for the contestants. Instead it is decided to eliminate all sequences containing an occurrence of two adjoining identical subsequences. Sequences containing such an occurrence will be called ``easy''. Other sequences will be called ``hard''. 

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
ABCDABCD 

Some examples of hard sequences are: 

D
DC
ABDAB
CBABCBA

Input

In order to provide the Quiz Master with a potentially unlimited source of questions you are asked to write a program that will read input lines that contain integers n and L (in that order), where n > 0 and L is in the range , and for each input line prints out the nth hard sequence (composed of letters drawn from the first L letters in the alphabet), in increasing alphabetical order (alphabetical ordering here corresponds to the normal ordering encountered in a dictionary), followed (on the next line) by the length of that sequence. The first sequence in this ordering is A. You may assume that for given n and L there do exist at least n hard sequences. 

For example, with L = 3, the first 7 hard sequences are: 


AB 
ABA 
ABAC 
ABACA 
ABACAB 
ABACABA 
As each sequence is potentially very long, split it into groups of four (4) characters separated by a space. If there are more than 16 such groups, please start a new line for the 17th group. 

Therefore, if the integers 7 and 3 appear on an input line, the output lines produced should be 

ABAC ABA
7
Input is terminated by a line containing two zeroes. Your program may assume a maximum sequence length of 80.

Sample Input


30 30 0

Sample Output


ABAC ABCA CBAB CABA CABC ACBA CABA28

Source


问题简介:

 输入正整数n和L,输出由前L个字符组成的、字典顺序第n小的不含相邻重复字串的字符串。不含相邻重复字串的字符串是指,一个字符串中,任意两个相邻的字串都不相等。输出结果时,对于找到的字符串,每4个字符间加入一个空格,每行输出80个字符。

思路分析:

大致的框架就是每次增加一个字符,在增加字符的同时去判断加上这个字符后是否产生了连续的相同子串,这也是很关键的一个点,如何判断是否产生了连续的相同子串,由于采用了回溯的思想,故以前的串是没有连续的相同子串的,故只需从后向前看包含了新字母后有无连续的相同子串。我们可以外层循环去遍历两个子串的长度,从1-(cur+1)/2(即当前串的一半),内层循环去遍历每一对子串的对应的点,如果在内层循环找到了一个点在两个子串上的对应位置不相等,则在此长度下没有产生连续的相同子串。于是,外层循环的长度加1,去判断下一个子串是否相同。如果找到了某一对连续的相同子串,则此次的分配不合理,尝试在该点分配下一个小于L的字母,如果没有找到一对连续的相同子串,则此次分配合理,可以递归下一个点,并且记录困难子串的变量加一。当变量等于n的时候则输出该序列即可。 由于无需返回值,所以直接return 即可。

package lianxi;

import java.util.Scanner;

public class KryptonFactorUva129 {
static Scanner scan = new Scanner(System.in);
static int C[]=new int [10000];
static int n,l,count=0;
public static void main(String[] args) {
while(scan.hasNext()){
n = scan.nextInt();
l = scan.nextInt();
getFactor(0);
for(int i=0;i<C.length;i++){
C[i] = 0;
}
}
}
private static int getFactor(int cur) { if(count++ == n){
for(int i = 0;i<cur;i++){
System.out.print((char)(C[i]+'A'));
}
System.out.println(); return 0;
} for(int i=0;i<l;i++){
C[cur] = i;
int ok = 1;
for(int j = 1;j*2<=cur+1;j++){
int equal = 1;
for(int k = 0;k < j;k++){
if(C[cur - k] != C[cur-k-j] ){
equal = 0; //找到一个不相同的点本次的测试子段即为困难的串
break;
}
}
if(equal == 1){
ok = 0;
break; //存在了相同的连续子串,尝试下一个字母
}
}
if(ok==1){
if(getFactor(cur+1) == 0){
return 0;
}
} }
return 1; } }


UVA129 Krypton Factor 困难的串 dfs回溯【DFS】的更多相关文章

  1. Krypton Factor 困难的串-Uva 129(回溯)

    原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...

  2. UVa 129 Krypton Factor困难的串 (dfs 递归搜索)

    回溯法,只需要判断当前串的后缀,而不是所有的子串 #include<iostream> #include<cstdio> using namespace std; ]; int ...

  3. uva129 - Krypton Factor 7.4.3 困难的串

      7.4.3困难的串 学习点:dfs加入返回值,递归搜索过程中如果有一个成功,就直接退出 //7.4.3 困难的串 #include<cstdio> #include<cstrin ...

  4. UVA129 —— Krypton Factor (氪因素)

    Input and Output In order to provide the Quiz Master with a potentially unlimited source of question ...

  5. 129 - Krypton Factor

    /*UVa129 - Krypton Factor --回溯问题.看例子可知道确定该字符串是按照从左到右依次考虑每个位置,当前位置填不上所有的字符时,需要回溯. -- */ #define _CRT_ ...

  6. UVA - 129 Krypton Factor (困难的串)(回溯法)

    题意:求由字母表前L个字母组成的字典序第n小的困难串.(如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串".) 分析:回溯时,检 ...

  7. UVa 129 Krypton Factor (DFS && 回溯)

    题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...

  8. UVa 129 Krypton Factor【回溯】

    学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...

  9. UVA-129 Krypton Factor(回溯)

    题目大意:由字母A到Z组成的字符串,其中有两个子串完全相同的叫做容易的串,反之叫困难的串.找出由前L个字母组成的第n个困难的串. 题目分析:简单回溯,不过要判断是否存在重复子串比较棘手.<入门经 ...

随机推荐

  1. 你的胃能Hold住未来的食物吗?

    ​ 如果你是一名美食客,那么一定会发现现在越来越多的食物已经发生了翻天覆地的变化,很多食物正在以我们未知的形式出现在生活中,其中最大的莫过于分子美食.你想过吗?当食物发生改变的时候,你的胃是否能够Ho ...

  2. Leetcode 141题 环形链表(Linked List Cycle) Java语言求解

    题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map ...

  3. 【深入理解Java虚拟机 】类加载器的命名空间以及类的卸载

    类加载器的命名空间 每个类加载器又有一个命名空间,由其以及其父加载器组成 类加载器的命名空间的作用和影响 每个类加载器又有一个命名空间,由其以及其父加载器组成 在每个类加载器自己的命名空间中不能出现相 ...

  4. Ubuntu18.04下安装mysql5.7超详细步骤

    1.首先执行下面三条命令: #安装mysql服务 sudo apt-get install mysql-server #安装客户端 sudo apt install mysql-client #安装依 ...

  5. 弹性盒子Flex Box滚动条原理,避免被撑开,永不失效

    在HTML中,要实现区域内容的滚动,只需要设定好元素的宽度和高度,然后设置CSS属性overflow 为auto或者scroll:   在Flex box布局中,有时我们内容的宽度和高度是可变的,无法 ...

  6. Nginx之反向代理配置(一)

    前文我们聊了下Nginx作为web服务器配置https.日志模块的常用配置.rewrite模块重写用户请求的url,回顾请参考https://www.cnblogs.com/qiuhom-1874/p ...

  7. 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解

    前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...

  8. 【沫沫金】使用Serv-U FTP服务,搭建文件服务器

    内网文件服务器安装Serv-U FTP 链接: https://pan.baidu.com/s/1G51D1enLqZCUhnprnjAITw 提取码: snah Java Web工程,引入 comm ...

  9. 【09】openlayers 图片图层

    效果:  创建地图: var map = new ol.Map({ //设置显示地图的视图 view: new ol.View({ projection:'EPSG:4326',//投影方式 cent ...

  10. Microsoft Visual Studio 修改语言包

    需求内容: 更改 Microsoft Visual Studio 界面的语言包(将中文改为英文) 解决方案: https://docs.microsoft.com/zh-cn/visualstudio ...