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

题意分析

搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可。

注意的地方就是合法串的判断,根据后缀的规则来判断,枚举后缀长度[1,len/2],后缀中是否有重复子串,若是的话表明不是合法串。

还有一个注意的地方,每次递归调用时,序号就要+1,无论是回溯回来的递归,还是深度搜索的递归,因为没找到一组可行解,编号就要加一。原因是此题不是用递归深度来判断输出的。

代码总览

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 100
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int s[nmax];
int ans[nmax],n,a,cot;
bool isfind;
void output(int num)
{
bool isnext = true;
for(int ct = 1;ct<num;++ct){
printf("%c",ans[ct]+'A'-1);
if(ct%4 == 0 && ct % 64 != 0 && ct!=num-1){printf(" "); isnext = false;}
else if( ct % 64 == 0) {printf("\n"); isnext = true;}
else isnext = false;
}
if(!isnext) printf("\n");
printf("%d\n",num-1);
}
void dfs(int num)
{
if(isfind) return;
if(cot == n){
output(num);
isfind = true;
return;
}
for(int i = 1; i<=a;++i){
ans[num] = i;
bool legal = true;
if(num != 1){
//check
// j 后缀长度
for(int j = 1;j<=num/2;++j){
int legal1 = true;// 当前后缀存在重复子串
for(int k = 0;k<j;++k){
if(ans[num-k] != ans[num-j-k]) legal1 = false;
}
if(legal1){
legal = false;
break;
}
}
if(isfind) return;
}
if(legal){
cot++;
dfs(num+1);
}
} }
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&a) &&(n||a)){
cot = 0;
isfind = false;
MEM(ans);
dfs(1);
}
return 0;
}

UVA.129 Krypton Factor (搜索+暴力)的更多相关文章

  1. UVa 129 Krypton Factor困难的串 (dfs 递归搜索)

    回溯法,只需要判断当前串的后缀,而不是所有的子串 #include<iostream> #include<cstdio> using namespace std; ]; int ...

  2. Uva 129 Krypton Factor

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

  3. UVa 129 Krypton Factor (DFS && 回溯)

    题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...

  4. UVa 129 Krypton Factor【回溯】

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

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

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

  6. 129 - Krypton Factor

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

  7. uva 129 krypton factors ——yhx

     Krypton Factor  You have been employed by the organisers of a Super Krypton Factor Contest in which ...

  8. UVA 129_ Krypton Factor

    题意: 一个字符串含有两个相邻的重复的子串,则称这个串为容易的串,其他为困难的串,对于给定n,l,求出由前l个字符组成的字典序第n小的困难的串. 分析: 按字典序在字符串末尾增加新的字符,并从当前字符 ...

  9. uva 129

    暴力求解 大致题意 如果一个字符串含有相邻的重复字串称为容易的串,反之为非容易 求字典序第n困难的串…… 大致思路,暴力如果是容易的串停过,然后困难的串继续求解tot++ 总之先记着吧…… 最后输出格 ...

随机推荐

  1. Android性能测试 | 启动时间篇

    [转载]原文地址:http://www.51testing.com/html/93/n-3724593.html 背景介绍 Android用户也许会经常碰到以下的问题: 1)应用后台开着,手机很快没电 ...

  2. python 水仙花

    #简单def narcissus(): for n in range(100, 1000, 1): a, b, c = n//100, (n//10)%10, (n%100)%10 if a ** 3 ...

  3. Python中assert的作用?

    1. assert 的作用是什么? assert这个关键字我们称之为“断言”,当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常. 什么情况下我们会需要这样的代码呢 ...

  4. TPO-10 C2 Return a literature book

    TPO-10 C2 Return a literature book 第 1 段 1.Listen to a conversation between a student and an employe ...

  5. 【picker】选择器组件说明

    picker从底部弹起选择器组件 组件细节: 1) 该组件有五种类型,分别是普通选择器.多列选择器.时间选择器.日期选择器.省市区选择器. 2) 组件内必需包裹内容,不然无法弹出选项 <!-- ...

  6. lintcode39 恢复旋转排序数组

    恢复旋转排序数组   给定一个旋转排序数组,在原地恢复其排序. 您在真实的面试中是否遇到过这个题? Yes 说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3 ...

  7. Wordcount -- MapReduce example -- Mapper

    Mapper maps input key/value pairs into intermediate key/value pairs. E.g. Input: (docID, doc) Output ...

  8. 6.hdfs的存储过程

    1.hdfs 怎么存储 切割存储 2. 为何每块是128m 与io读写速度有关,一般人的接受速度1s中,而磁盘的读写速度为100m/s,在读取文件时候需要硬盘寻找地址,一般读懂速度和寻找之间的比例是1 ...

  9. 《剑指Offer》题十一~题二十

    十一.旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如,数组{3, 4, 5, 1, 2}为{ ...

  10. Python练习—函数

    1.编写函数f(n),实现输入n的值,求出n的阶乘.然后调用此函数计算1! +2!+3!+……10!的结果,输出到屏幕上. def f(n): count = 1; for i in range(1, ...