回溯(UVA129)
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)的更多相关文章
- 回溯-uva129
题目链接:https://vjudge.net/problem/UVA-129 题解: 这道题卡了一会儿的时间,一开始最大的问题是如何判断添加了一个字符之后,该字符串是不是一个困难的串,解决办法是:利 ...
- UVA129 Krypton Factor 困难的串 dfs回溯【DFS】
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 7_5 困难的串(UVa129)<回溯法:避免无用判断>
“超级氪因素大赛”(译注:英国的一档电视心智竞答节目)的主办方雇你来对付那些足智多谋的参赛选手.在比赛的一个环节中,节目主持人将念出一长串的字母来考验选手的记忆能力.因为许多选手都是分析字串模式的高手 ...
- N皇后问题—初级回溯
N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...
- jQuery 2.0.3 源码分析 回溯魔法 end()和pushStack()
了解了jQuery对DOM进行遍历背后的工作机制,可以在编写代码时有意识地避免一些不必要的重复操作,从而提升代码的性能 从这章开始慢慢插入jQuery内部一系列工具方法的实现 关于jQuery对象的包 ...
- linux中oops信息的调试及栈回溯【转】
本文转载自:http://blog.csdn.net/kangear/article/details/8217329 ========================================= ...
- Java数据结构之回溯算法的递归应用迷宫的路径问题
一.简介 回溯法的基本思想是:对一个包括有很多结点,每个结点有若干个搜索分支的问题,把原问题分解为对若干个子问题求解的算法.当搜索到某个结点.发现无法再继续搜索下去时,就让搜索过程回溯(即退回)到该结 ...
- 回溯 DFS 深度优先搜索[待更新]
首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权! 1.概念 回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...
- 46. Permutations 回溯算法
https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...
随机推荐
- struts2+Hibernate4+spring3+EasyUI环境搭建之五:引入jquery easyui
1.下载jquery easyui组件 http://www.jeasyui.com/download/index.php 2.解压 放到工程中 如图 3.jsp引入组件:必须按照如下顺序 ...
- [iOS 多线程 & 网络 - 2.8] - 检测网络状态
A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...
- 从工程中删除Cocoapods
从工程中删除Cocoapods 分类: Xcode iOS 2013-08-24 01:11 5512人阅读 评论(2) 收藏 举报 CocoapodsiOSXcode 1. 删除工程文件夹下的Pod ...
- UVaLive 7363 A Rational Sequence (二叉树)
题意:给定一个二叉树,并对每一个进行编号和规定,现在给你一个值,问你是第几个. 析:这个题,我想了好久才想出来,这个真是数据结构练的太差了,不够扎实,这个题,应该从下向上推,如果分子大于分母,那么这个 ...
- HDU 4431 Mahjong (DFS,暴力枚举,剪枝)
题意:给定 13 张麻将牌,问你是不是“听”牌,如果是输出“听”哪张. 析:这个题,很明显的暴力,就是在原来的基础上再放上一张牌,看看是不是能胡,想法很简单,也比较好实现,结果就是TLE,一直TLE, ...
- Java二维码登录流程实现(包含短地址生成,含部分代码)
近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及的那些事儿. 二维码原理 二 ...
- win7常用快捷键
Win+1:打开/显示超级任务栏第一个图标代表的程序Win+2:打开/显示超级任务栏第二个图标代表的程序(3.4.……如此类推)Win+Tab:3D切换窗口,你要是按住不松口,则所有窗口会轮流翻转Wi ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
- C++中使用union的几点思考(转)
C++中使用union的几点思考 大卫注:这段时间整理旧资料,看到一些文章,虽然讲的都是些小问题,不大可能用到,但也算是一个知识点,特整理出来与大家共享.与此相关的那篇文章的作者的有些理解是错误的,我 ...