UVA-129 Krypton Factor(回溯)
题目大意:由字母A到Z组成的字符串,其中有两个子串完全相同的叫做容易的串,反之叫困难的串。找出由前L个字母组成的第n个困难的串。
题目分析:简单回溯,不过要判断是否存在重复子串比较棘手。《入门经典》上借鉴八皇后问题,只判断添进字符后是否存在连续子串。具体做法是这样的,以长度为对象枚举以新添进字符为尾巴的子串,看是否重复。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; int n,k,cnt,path[1000]; void dfs(int cur)
{
if(cur>81)
return ;
if(cnt++==n){
for(int i=0;i<cur;++i){
printf("%c",char(path[i]+'A'));
if(i%4==3&&i!=cur-1){
if(i%64==63)
printf("\n");
else
printf(" ");
}
}
printf("\n%d\n",cur);
return ;
}
for(int i=0;i<k;++i){
path[cur]=i;
int flag=1;
for(int j=1;j*2<=cur+1;++j){
int ok=1;
for(int l=0;l<j;++l)
if(path[cur-l]!=path[cur-l-j]){
ok=0;
break;
}
if(ok){
flag=0;
break;
}
}
if(flag)
dfs(cur+1);
if(cnt>n)
return ;
}
} int main()
{
while(scanf("%d%d",&n,&k)&&(n+k))
{
cnt=0;
dfs(0);
}
return 0;
}
UVA-129 Krypton Factor(回溯)的更多相关文章
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- UVa 129 Krypton Factor【回溯】
学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...
- UVA - 129 Krypton Factor (困难的串)(回溯法)
题意:求由字母表前L个字母组成的字典序第n小的困难串.(如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串".) 分析:回溯时,检 ...
- UVa 129 Krypton Factor (DFS && 回溯)
题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...
- UVa 129 Krypton Factor困难的串 (dfs 递归搜索)
回溯法,只需要判断当前串的后缀,而不是所有的子串 #include<iostream> #include<cstdio> using namespace std; ]; int ...
- Uva 129 Krypton Factor
0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...
- 129 - Krypton Factor
/*UVa129 - Krypton Factor --回溯问题.看例子可知道确定该字符串是按照从左到右依次考虑每个位置,当前位置填不上所有的字符时,需要回溯. -- */ #define _CRT_ ...
- uva 129 krypton factors ——yhx
Krypton Factor You have been employed by the organisers of a Super Krypton Factor Contest in which ...
- UVA 129_ Krypton Factor
题意: 一个字符串含有两个相邻的重复的子串,则称这个串为容易的串,其他为困难的串,对于给定n,l,求出由前l个字符组成的字典序第n小的困难的串. 分析: 按字典序在字符串末尾增加新的字符,并从当前字符 ...
- UVA129 Krypton Factor 困难的串 dfs回溯【DFS】
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Code Forces 652C Foe Pairs
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 沈阳网络赛K-Supreme Number【规律】
26.89% 1000ms 131072K A prime number (or a prime) is a natural number greater than 11 that cannot be ...
- talib 中文文档(九):# Volatility Indicator Functions 波动率指标函数
Volatility Indicator Functions 波动率指标函数 ATR - Average True Range 函数名:ATR 名称:真实波动幅度均值 简介:真实波动幅度均值(ATR) ...
- Python开发【笔记】:探索Python F-strings
F-strings 在python3.6.2版本中,PEP 498 提出一种新型字符串格式化机制,被称为“字符串插值”或者更常见的一种称呼是F-strings(主要因为这种字符串的第一个字母是f) 简 ...
- oracle(十一) scn
SCN(System Chang Number)作为oracle中的一个重要机制,在数据恢复.Data Guard.Streams复制.RAC节点间的同步等各个功能中起着重要作用. 理解SCN的运作机 ...
- IQKeyboardManager第三方库的使用
IQKeyboardManager是iOS中解决键盘弹起遮挡UITextField/UITextView的一种很实用的工具.无需输入任何代码,不需要额外的设置.使用IQKeyboardManager的 ...
- andorid ListView和GirdView 与ScrollView 冲突
1.listview解决方法 public static void setListViewHeightBasedOnChildren(ListView listView) { if(listView ...
- scRNA-seq测序的两种技术[转载]
转自:http://www.ebiotrade.com/newsf/2017-9/201795172237350.htm 1.综述 哈佛大学的两个团队将微流体技术引入单细胞RNA-Seq方法中,分别开 ...
- (1)R介绍
1. R初窥 从CRAN(The Comprehensive R Archive Network)cran.r-project.org—mirrors.html中选择一个镜像,然后下载合适的安装包(R ...
- MapReduce: map读取文件的过程
我们的输入文件 hello0, 内容如下: xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown 逻辑上有3条记录, 它们以 ...