回溯法:避免无用判断,强化回溯代码的实现过程

题目的大意就是以字典序为准,排列字符串,但要保证一个字符串中不包含相邻的重复子串。

Problem Description

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:

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

在判断当前字符串是否已经存在连续的重复子串,例如判断ABACABA是否包含连续重复子串,并不需要说检查该字符串所有长度为偶数的子串,仅需要做的是判断当前串的后缀。

另外对回溯法来说,一旦本次所要赋值的字符不满足,并且在循环了所有可以在这一次赋值的字符后,仍未满足,就要回溯到上一层,这种情况首先要保证成功次数不要增加(可以将次数设置为全局变量,当然进行完这一组数据后,要记得重新赋值,避免后面的测试数据出现问题,通常全局变量都要在完成一组数据测试后,重新赋初始值)

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 200
using namespace std;
char alp[MAXN]="AABCDEFGHIJKLMNOPQRSTUVWXYZ";//首先打表方便后面的操作
char ch[MAXN]="#";//处理一个小的细节,当第一个字符时,其实不需要判断前面的字符,为了统一操作,在ch[0]处赋一个与第一个字符一定不同的量
int c=;
bool tag=false;//全局变量,完成字符串的标志
void dfs(int n,int t,int cur)
{
int i,j,k=,m,s;
bool flag=true;
if(n==c)//以次数作为完成的标志
{
while(k<=cur-)
{
if(k==)
cout<<endl;
if(k%==&&k!=&&k!=cur-)//处理格式要求
cout<<ch[k]<<' ';
else
cout<<ch[k];
k++;
}
cout<<endl;
cout<<cur-<<endl;
tag=true;//以标示符作为字符串完成的标志
}
else
{
for(i=;i<=t;i++)//循环此次可以赋值的字符
{
flag=true;
ch[cur]=alp[i];
for(j=cur/;j<=cur-;j++)//字符串判重
{
s=;
m=cur-j-;
for(k=;k<=m;k++)
{
if(ch[cur-k]==ch[j-k])
s++;
}
if(s==m+)
{
flag=false;
break;
}
}
if(flag==false)
continue;
if(!tag)
{
c++;
dfs(n,t,cur+);//满足,则字符的长度加一,这也满足字典序的要求,若回溯到这里,那么即回头继续循环可以满足的字符,相当于说字符长度加一个不满足,那么前一个字符往后改,就如同从ABC->ABD(原本要走ABCD的)
}
if(tag)
{
// cout<<"return ";
return ;//若已完成则一步步回头
}
}
}
}
int main()
{
int n,l;
while(cin>>n>>l)
{
if(n==&&l==)
break;
else
dfs(n,l,);
tag=false;//全局变量重新赋初始值
c=;// 全局变量重新赋初始值
}
return ;
}

HDU 1627 Krypton Factor的更多相关文章

  1. hdu - 1627 Krypton Factor (dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. ...

  2. Krypton Factor

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

  3. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

  4. 129 - Krypton Factor

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

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

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

  6. hdu 5428 The Factor 分解质因数

    The Factor  Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...

  7. HDU 5428 The Factor 分解因式

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5428 The Factor  Accepts: 101  Submissions: 811  Tim ...

  8. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

  9. hdu 5428 The Factor(数学)

    Problem Description There is a sequence of n positive integers. Fancycoder is addicted to learn thei ...

随机推荐

  1. openpyxl的使用记录

    脚本功能描述: 读取指定文件夹内的.xlsx文件,遍历提取整理信息保存到另一指定文件夹中 import openpyxl import os import shutil city='城市名' def ...

  2. 百科知识 ass文件如何打开

    直接拖入视频即可播放 鼠标右键 用记事本打开 也有一些软件支持比如POPSUB(也比较方便调整时间轴) 如果你是说如何加载字幕的话 用VOBSUB是最好的... ASS是视频的字幕,和视频放在同一文件 ...

  3. python(7)- 小程序练习:循环语句for,while实现99乘法表

    打印99乘法表 for 循环语句实现: for i in range(1,10): for j in range(1,10): print(j,"x",i,"=" ...

  4. 爬虫基本操作、requests和BeautifulSoup

    1. 爬虫基本操作 例如舆情系统: 获取汽车之家新闻放到自己数据库里,创建自己的app,发布内容,注明来源,自己创业. URL指定内容获取到 - 发送Http请求:http://www.autohom ...

  5. vs2010中添加dll文件

    1.更改设置 1.1   project->properties->configuration properties->C/C++->General->Addtional ...

  6. Django-models-m2m

    在Django的orm中,如果有多对多的字段,那么他的第三张表时自己生成的,参考官方文档发现第三张表可以自己写↓: 而且第三张表好像是可以自定制的 from django.db import mode ...

  7. mysql导入数据库_仅仅用frm向mysql导入表结构

    网上一个连接mysql的jsp代码段,给了数据库的备份文件.可是仅仅有frm, mysql的每张表有三个文件.各自是,*.frm是描写叙述了表的结构.*.MYD保存了表的数据记录.*.MYI则是表的索 ...

  8. Struts2拦截器 解决登录问题

    一.了解Struts2 拦截器[Interceptor] 拦截器的工作原理如图  拦截器是由每一个action请求(request)都包装在一系列的拦截器的内部,通过redirectAction再一次 ...

  9. LINQ to SQL 语句(1)之 Where

    LINQ to SQL 语句(1)之 Where Where 操作 适用场景:实现过滤,查询等功能. 说明:与 SQL 命令中的 Where 作用相似,都是起到范围限定也就是过滤作用的 ,而判断条 件 ...

  10. php 实现简单加入购物车

    今天在练习购物车以及提交订单,写的有点头晕,顺便也整理一下,这个购物车相对来说比较简单,用于短暂存储,并没有存储到数据库, 购物车对于爱网购的人来说简直是熟悉的不能再熟悉了,在写购物车之前,我们首先要 ...