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. 程序员的冷笑话 python版本

    在伯乐在线上看到了个冷笑话,感觉很有意思. void tellStory() { printf("从前有座山\n"); printf("山上有座庙\n"); p ...

  2. 「日常训练」Girls and Boys(HDU-1068)

    题意 有n个同学,给出同学之间的爱慕关系,选出一个集合使得集合中的人没有爱慕关系.问能选出的最大集合是多少. 分析 二分图的最大独立集. 最大独立集的意思是,在图中选出最多的点,使他们两两之间没有边, ...

  3. 初学Direct X(10)—— D3D基础预备知识

    初学Direct X(10) -- D3D基础预备知识 1. 像素格式 D3DFMT_X8R8G8B8(F) X:未加使用 8:8位用于显示 B:用于显示蓝色 F:浮点像素类型 以下三个较为常用,使用 ...

  4. js 加密 crypto-js des加密

    js 加密 crypto-js    https://www.npmjs.com/package/crypto-js   DES  举例:   js 引入:   <script src=&quo ...

  5. informix如何查询第一条记录

    1.select first 1 * from shop; 正序查询第一条数据 2.select first 1 * from shop order by create_time desc; 按创建时 ...

  6. 【shell 练习1】编写Shell条件句练习

    实例一.比较两个整数大小 #!/bin/bash while true do read -p "Please input two int nums:" a b >/dev/& ...

  7. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

  8. BZOJ 4815 CQOI2017 小Q的表格 欧拉函数+分块

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 题意概述:要认真概述的话这个题就出来了... 分析: 首先分析题目,认真研究一下修 ...

  9. HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  10. js经典试题之数组与函数

    js经典试题之数组与函数 1:列举js的全局函数? 答案:JavaScript 中包含以下 7 个全局函数escape( ).eval( ).isFinite( ).isNaN( ).parseFlo ...