UVA - 129 Krypton Factor (困难的串)(回溯法)
题意:求由字母表前L个字母组成的字典序第n小的困难串。(如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的串”。)
分析:回溯时,检查枚举的当前串是否为困难串的方法:将最后一个字母(下标为cur)与第cur-j个字母不断依次向前比较j个字母。
采用此种方法的原因是,前面的串都已经是回文串。
例如:ABAC,不需检查每个长度为偶数的串是否符合要求,因为枚举的每一步都保证是困难串,所以长度为2的串只需检查AC,无需检查AB,BA。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int n, L;
int ans[MAXN];
int cnt;
int dfs(int cur){
if(cnt++ == n){//cnt为当前为第几串
for(int i = ; i < cur; ++i){
if(i && (i % == )) printf("\n");
else if(i && (i % == )) printf(" ");
printf("%c", 'A' + ans[i]);
}
printf("\n");
printf("%d\n", cur);
return ;
}
else{
for(int i = ; i < L; ++i){
ans[cur] = i;
bool ok = true;
for(int j = ; j * <= cur + ; ++j){//cur+1为当前串的长度,检查后缀最多只用检查到(cur+1)/2,因为再往前检查,检查的前串短于后串,没必要检查
bool flag = true;
for(int k = ; k < j; ++k){
if(ans[cur - k] != ans[cur - k - j]){
flag = false;
break;
}
}
if(flag){//方案不合法
ok = false;
break;
}
}
if(ok){//方案合法继续递归
if(!dfs(cur + )) return ;//已经找到解,退出所有递归
}
}
return ;
}
}
int main(){
while(scanf("%d%d", &n, &L) == ){
if(!n && !L) return ;
cnt = ;
memset(ans, , sizeof ans);
dfs();
}
return ;
}
UVA - 129 Krypton Factor (困难的串)(回溯法)的更多相关文章
- UVa 129 Krypton Factor困难的串 (dfs 递归搜索)
回溯法,只需要判断当前串的后缀,而不是所有的子串 #include<iostream> #include<cstdio> using namespace std; ]; int ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- Krypton Factor 困难的串-Uva 129(回溯)
原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...
- UVA129 Krypton Factor 困难的串 dfs回溯【DFS】
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- UVa 129 Krypton Factor【回溯】
学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...
- UVa 129 Krypton Factor (DFS && 回溯)
题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...
- Uva 129 Krypton Factor
0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...
- uva 129 krypton factors ——yhx
Krypton Factor You have been employed by the organisers of a Super Krypton Factor Contest in which ...
- 129 - Krypton Factor
/*UVa129 - Krypton Factor --回溯问题.看例子可知道确定该字符串是按照从左到右依次考虑每个位置,当前位置填不上所有的字符时,需要回溯. -- */ #define _CRT_ ...
随机推荐
- Android中的分页加载
//----------------------MainActivity中--------------------------------------------------- package com ...
- [IDL入门] 两个PPT,IDL上手
首先看看IDL能干什么,<Solving Real Problems with Computer Graphics>ppt是英文的,很精彩. 下载地址:http://pan.baidu.c ...
- yii migrate 设计博客
yii migrate/create create_blog_table该命令生成的迁移文件位于 advanced\console\migrations 目录,可能你已经注意到了,yii migrat ...
- JS的数据类型转换
JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...
- 关于SVN 提交一半卡死的问题
解决方案 1:将项目刷新一下 2:然后在到 project - clean 一下项目 3:之后提交svn 不过暂时不能确认 要等下方进度条的 svn更新状态完成 在点击确认进行提交 4:Buildin ...
- UIColor -colorWithAlphaComponent
view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7]; //颜色透明度
- VIEWCONTROLLER的启动流程
转载自:http://sunnyyoung.net/post/ios/2015-04-22-viewcontrollerde-qi-dong-liu-cheng-yu-jie-xi VIEWCONTR ...
- UIView你知道多少
转载自:http://www.cnblogs.com/likwo/archive/2011/06/18/2084192.html 曾经有人这么说过,在iphone里你看到的,摸到的,都是UIVie ...
- PHP :Call to undefined function mysql_connect()
今天配置apache ,php,mysql 的时候,一直报(Call to undefined function mysql_connect()),PHP一直连接不上数据库,从网上查,答案也都是千篇一 ...
- Quartz总结(四):动态修改定时器二
前文:http://www.cnblogs.com/LiuChunfu/p/5598806.html 提到了一种动态修改定时器的方法, 其本质就是在job方法中注入Schedular的对象,从Sche ...