uva129 - Krypton Factor 7.4.3 困难的串
7.4.3困难的串
学习点:dfs加入返回值,递归搜索过程中如果有一个成功,就直接退出
//7.4.3 困难的串
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,L;
int cnt;
char v[81]; bool judge(int cur)
{
for(int i=1;i<=(cur+1)/2;i++)//判断长度为2*i的后缀是否有相同的
{
bool equal=1;
for(int j=0;j<i;j++)
if(v[cur-i-j]!=v[cur-j])
{
equal=0;
break;
}
if(equal)
return false;
}
return true;
} int dfs(int cur)
{
//printf("cur: %d\n", cur);
for(int i=0;i<L;i++)
{
v[cur]='A'+i;
if(judge(cur))
{
cnt++;
if(cnt==n)
{
v[cur+1]=0;
printf("%s\n", v);
return 1;
}
if(dfs(cur+1))
return 1;
}
}
return 0;
} int main()
{
while(cin>>n>>L)
{
cnt=0;
dfs(0);
}
return 0;
}
修改一下输出格式即可解决uva129
//7.4.3 困难的串
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,L;
int cnt;
char v[81]; bool judge(int cur)
{
for(int i=1;i<=(cur+1)/2;i++)//判断长度为2*i的后缀是否有相同的
{
bool equal=1;
for(int j=0;j<i;j++)
if(v[cur-i-j]!=v[cur-j])
{
equal=0;
break;
}
if(equal)
return false;
}
return true;
} void output(int cur)
{
for(int i=0;i<=cur;i++)
{
if(i%4==0 && i>0)
{
if(i%64==0 && i>0)
putchar('\n');
else
putchar(' ');
}
putchar(v[i]);
}
printf("\n%d\n", cur+1);
}
int dfs(int cur)
{
//printf("cur: %d\n", cur);
for(int i=0;i<L;i++)
{
v[cur]='A'+i;
if(judge(cur))
{
cnt++;
if(cnt==n)
{
v[cur+1]=0;
//printf("%s\n", v);
output(cur);
return 1;
}
if(dfs(cur+1))
return 1;
}
}
return 0;
} int main()
{
while(cin>>n>>L && n && L)
{
cnt=0;
dfs(0);
}
return 0;
}
uva129 - Krypton Factor 7.4.3 困难的串的更多相关文章
- UVA129 Krypton Factor 困难的串 dfs回溯【DFS】
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- UVA129 —— Krypton Factor (氪因素)
Input and Output In order to provide the Quiz Master with a potentially unlimited source of question ...
- 129 - Krypton Factor
/*UVa129 - Krypton Factor --回溯问题.看例子可知道确定该字符串是按照从左到右依次考虑每个位置,当前位置填不上所有的字符时,需要回溯. -- */ #define _CRT_ ...
- Krypton Factor 困难的串-Uva 129(回溯)
原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...
- UVA - 129 Krypton Factor (困难的串)(回溯法)
题意:求由字母表前L个字母组成的字典序第n小的困难串.(如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串".) 分析:回溯时,检 ...
- Krypton Factor
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- Uva 129 Krypton Factor
0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...
- UVa 129 Krypton Factor【回溯】
学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...
随机推荐
- apple-touch-icon,shortcut icon和icon的区别
apple-touch-icon 可以了解到这是一个类似网站favicon的图标文件,用来在iphone和ipod上创建快捷键时使用. 这个文件应当是png格式,57x57像素大小,放在网站根目录之下 ...
- LoadRunner中常见参数和变量
1.参数和字符串变量的交换 ①lr_save_string(“hello world”,“param”) 将hello world 保存在参数 param中 ②lr_eval_stri ...
- TestNG中同一个类中执行多个test()方法如何配置testng.xml
public class IndexInfo extends BaseTesting{ private IndexPage IndexPage1;// private AddEquipmentInfo ...
- 一个DataTable赋值给另一个DataTable的常用方法
DataView view = new DataView();view.Table = DataTableA;view.RowFilter = "itemType = 'book'" ...
- bzoj1193
#include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> using ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- [POJ] #1008# Maya Calendar : 字符处理/同余问题
一. 题目 Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74085 Accepted: 2 ...
- WebRTC源码分析:音频模块结构分析
一.概要介绍WebRTC的音频处理流程,见下图: webRTC将音频会话抽象为一个通道Channel,譬如A与B进行音频通话,则A需要建立一个Channel与B进行音频数据传输.上图中有三个Chann ...
- Improved logging in Objective-C
[Improved logging in Objective-C] Example of logging the current method and line number. Paste it in ...
- 动态生成xml文件
使用xmlParser动态生成xml,输入的字符编码是gbk,结果怎么生成都不行,后来把输入转成utf8之后, 再生成就ok了