POINT:  如何判断是否包含连续重复子串?  判断 当前串 的 后缀 啦~~~

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 sequence 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 and Output

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:

A 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 3
0 0

Sample Output

ABAC ABCA CBAB CABA CABC ACBA CABA
28 回溯比较有代表性的题+dfs搜素,刘汝佳AOAPCⅡ的例题,第一次比着标程抄的,当时理解了,后来发现回头再看还是没有头绪,事实证明果然还是没掌握。这是第二遍,方法细节要记住理解,希望第三次做时能顺利写出来。 没管格式,想直接贴代码的等着WA吧。╮(╯▽╰)╭
 #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std; #define INF 0xffffff7
#define maxn 100000+10
int cnt;
int n, l;
int S[maxn];
int dfs(int cur)//返回0表示已经得到解,无需继续搜索
{
if(cnt++ == n)
{
for(int i = ; i < cur; i++)
printf("%c", 'A'+S[i]);
printf("\n");
return ;
}
for(int i = ; i < l; i++)
{
S[cur] = i; int ok = ;
for(int j = ; j* <= cur+; j++)//尝试长度为j的后缀,后缀长度不能长于当前串的一半
{
int equal = ;
for(int k = ; k < j; k++)
{
if(S[cur-k] != S[cur-k-j])
{
equal = ; break;//若长度是j的后缀能形成困难的串,则j++,继续循环
}
}
if(equal) {ok = ; break;}//反之,若能构成简单串,则直接退出循环;
}
//如果判断存在后缀能形成简单的串,则ok = 0,继续为S[cur]尝试下一个字符。
//反之,所有后缀都不会形成简单串,则继续下一位置(cur+1);
if(ok)
if(!dfs(cur+)) return ;
}
return ;
} int main()
{
scanf("%d%d", &n, &l);
dfs();
return ;
}
												

回溯(UVA129)的更多相关文章

  1. 回溯-uva129

    题目链接:https://vjudge.net/problem/UVA-129 题解: 这道题卡了一会儿的时间,一开始最大的问题是如何判断添加了一个字符之后,该字符串是不是一个困难的串,解决办法是:利 ...

  2. UVA129 Krypton Factor 困难的串 dfs回溯【DFS】

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

  3. 7_5 困难的串(UVa129)<回溯法:避免无用判断>

    “超级氪因素大赛”(译注:英国的一档电视心智竞答节目)的主办方雇你来对付那些足智多谋的参赛选手.在比赛的一个环节中,节目主持人将念出一长串的字母来考验选手的记忆能力.因为许多选手都是分析字串模式的高手 ...

  4. N皇后问题—初级回溯

    N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...

  5. jQuery 2.0.3 源码分析 回溯魔法 end()和pushStack()

    了解了jQuery对DOM进行遍历背后的工作机制,可以在编写代码时有意识地避免一些不必要的重复操作,从而提升代码的性能 从这章开始慢慢插入jQuery内部一系列工具方法的实现 关于jQuery对象的包 ...

  6. linux中oops信息的调试及栈回溯【转】

    本文转载自:http://blog.csdn.net/kangear/article/details/8217329 ========================================= ...

  7. Java数据结构之回溯算法的递归应用迷宫的路径问题

    一.简介 回溯法的基本思想是:对一个包括有很多结点,每个结点有若干个搜索分支的问题,把原问题分解为对若干个子问题求解的算法.当搜索到某个结点.发现无法再继续搜索下去时,就让搜索过程回溯(即退回)到该结 ...

  8. 回溯 DFS 深度优先搜索[待更新]

      首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权!   1.概念   回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...

  9. 46. Permutations 回溯算法

    https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...

随机推荐

  1. 采用现代Objective-C

    多年来,Objective-C语言已经有了革命性的发展.虽然核心理念和实践保持不变,但语言中的部分内容经历了重大的变化和改进.现代化的Objective-C在类型安全.内存管理.性能.和其他方面都得到 ...

  2. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  3. 应用反射写的tostring方法

    应用反射写的tostring方法 应用反射写的tostring方法,方便以后查询 代码 package com.chzhao.reflecttest; import java.lang.reflect ...

  4. POJ1228(稳定凸包问题)

    题目:Grandpa's Estate   题意:输入一个凸包上的点(没有凸包内部的点,要么是凸包顶点,要么是凸包边上的点),判断这个凸包是否稳定.所谓稳 定就是判断能不能在原有凸包上加点,得到一个更 ...

  5. this指针和m_hWnd的区别

    m_hWnd ① m_hWnd这个成员变量,最早是定义在类CWnd中,而且是类CWnd的第一个数据成员, 先看一下MSDN的解析: The handle of the Windows window a ...

  6. 如何关闭dell inspiron n4010的内置麦克

    如何关闭dell inspiron n4010的内置麦克 dell inspiron n4010这款电脑的内置麦克是默认开启的,如果你的扩音器音量开得稍大,当你打字的时候就会听到回音,最讨厌的是,当你 ...

  7. hibernate一对多关系配置

    一.     表信息 公司表 cId cName cAdress Null Null Null 表t_company 员工表 sId sName sAge cId Null Null Null Nul ...

  8. ecshop收货人信息中修改手机号为必填

    Ecshop 修改收货人信息 把电话改成选择填写 手机改为必填 (加强版) 1.  编辑根目录/js/utils.js 增加手机号码的正则表达式 参照Utils.isTel = function ( ...

  9. apache win openssl

    Rubayat Hasan Software Development, Music, Web Design, life, thoughts…   Home Portfolio Projects Con ...

  10. 关于STM32的ST官方的库的一点看法

    标题确实很别扭,因为我现在用这个库也很别扭. 在不久之前,一直有个讨论的话题:STM32开发是用库还是用寄存器? 很多人的结论是不需要讨论! 但是,今天我想说下我的看法. 首先,我还是一个菜鸟,对库对 ...