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 困难的串的更多相关文章

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

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

  2. UVA129 —— Krypton Factor (氪因素)

    Input and Output In order to provide the Quiz Master with a potentially unlimited source of question ...

  3. 129 - Krypton Factor

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

  4. Krypton Factor 困难的串-Uva 129(回溯)

    原题:https://uva.onlinejudge.org/external/1/129.pdf 按照字典顺序生成第n个“困难的串” “困难的串”指的是形如ABAB, ABCABC, CDFGZEF ...

  5. UVA - 129 Krypton Factor (困难的串)(回溯法)

    题意:求由字母表前L个字母组成的字典序第n小的困难串.(如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串".) 分析:回溯时,检 ...

  6. Krypton Factor

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

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

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

  8. Uva 129 Krypton Factor

    0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...

  9. UVa 129 Krypton Factor【回溯】

    学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...

随机推荐

  1. Android无法调用JS的问题解决

    1.启用JS webView.getSettings().setJavaScriptEnabled(true); 2.需要使用WebChromeClient(如果没有这个Client,很多东西不会响应 ...

  2. linearlayout 水平垂直居中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  3. 嵌入式 fork与vfork的区别

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1.  fork  ():子进程拷贝父进程的数据段,代码段     vfork ( ):子进程与父进程共享数据段 ...

  4. Php 笔记2-----手机端 与 php服务器的通信

    对于 手机端 和 php服务器的通信,是不存在表单这一概念的  ,除非自己去实现, 所以通常情况下步骤是: 假定上传的是字符串. 1  手机端的流程是 把文件或者字符串,转化为 特定的流. 2 通过h ...

  5. MySQL_PHP学习笔记_2015_0923_MySQL如何开启事件

    1. 查看事件状态>>>>>>>>>>>>>>>>>>>>>>> ...

  6. Asp.Net学习进度备忘(第一步:ASP.NET Web Forms)

    书签:“Web Pages”和“MVC”跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.ASP. ...

  7. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  8. bzoj 3594 [Scoi2014]方伯伯的玉米田(DP+二维BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3594 [题意] 给定一个n个数的序列,有K次将一个区间内的数加1的机会,问最长不下降子 ...

  9. WebGoat学习——跨站请求伪造(Cross Site Request Forgery (CSRF))

    跨站请求伪造(Cross Site Request Forgery (CSRF)) 跨站请求伪造(Cross Site Request Forgery (CSRF))也被称为:one click at ...

  10. 代理(Proxy)模式简介

    Proxy 模式简介 代理模式的两个应用: 打开文档时加载大图片 例如:如果有个对象是一张很大的图片,而这张图片需要花费很长时间才能显示出来,那么当这个图片包含在文档中的后面时,使用编辑器或浏览器打开 ...