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. (转载)JDOM/XPATH编程指南

    JDOM/XPATH编程指南 本文分别介绍了 JDOM 和 XPATH,以及结合两者进行 XML 编程带来的好处. 前言 XML是一种优秀的数据打包和数据交换的形式,在当今XML大行于天下,如果没有听 ...

  2. 使用truss、strace或ltrace诊断软件的“疑难杂症”

    简介 进程无法启动,软件运行速度突然变慢,程序的"Segment Fault"等等都是让每个Unix系统用户头痛的问题,本文通过三个实际案例演示如何使用truss.strace和l ...

  3. MySQL Update语句用法

    用一个表的某列值更新另外一个表的某列值的sql语句: update tableA a innner join tableB b on a.column_1 = b.column_1 set a.col ...

  4. struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

    成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...

  5. 《JavaScript高级程序设计》 读书笔记(二)

    数据类型 ECMAScript 中有 5 种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和 String.还有 1 种复杂数据类型--Object,O ...

  6. RFID与射频卡电器特性

    电气特性: 容量为8K位EEPrOM: ● 分为16个扇区,每个扇区为4块,每块16个字节,以块为存取单位: ● 每个扇区有独立的一组密码及访问控制: ● 每张卡有唯一序列号,为32位: ● 具有防冲 ...

  7. 最小投票BZOJ 1934([Shoi2007]Vote 善意的投票-最小割)

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下最小投票 1934: [Shoi2007]Vote 好心的投票 Time Limit: 1 Sec Memory L ...

  8. 开始学习web前端技术

    不能再蹉跎了,不能再徘徊了,不能再犹豫了,犹豫徘徊等于白来…… 感觉之前浪费了太多的岁月,必须得学习一门实用的技术来充实自己空虚的心情了. 想来想去网页应该是万金油的,大大小小多多少少都得用到.既然如 ...

  9. Middleware课程01-概述

    中间件是一种独立的系统软件或服务程序,分布式应用软件借助这种软件在不同的技术之间共享资源.中间件位于客户机/ 服务器的操作系统之上,管理计算机资源和网络通讯.是连接两个独立应用程序或独立系统的软件.相 ...

  10. JQuery Basic Features Quick Walkthrough

    1. Basic Selectors $('p')—Accesses all the paragraph elements in the HTML file $('div')—Accesses all ...