当前位置:网站首页>Krypton Factor-紫书第七章暴力求解
Krypton Factor-紫书第七章暴力求解
2022-07-05 22:51:00 【程序的搬运工Hunter】
目录
题目描述
题目背景
You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physical abilities. In one section of the contest the contestants are tested on their ability to recall a sequenace of characters which has been read to them by the Quiz Master. Many of the contestants are very good at recognising patterns. Therefore, in order to add some difficulty to this test, the organisers have decided that sequences containing certain types of repeated subsequences should not be used. However, they do not wish to remove all subsequences that are repeated, since in that case no single character could be repeated. This in itself would make the problem too easy for the contestants. Instead it is decided to eliminate all sequences containing an occurrence of two adjoining identical subsequences. Sequences containing such an occurrence will be called “easy”. Other sequences will be called “hard”.
For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are:
- BB
- ABCDACABCAB
- ABCDABCD
Some examples of hard sequences are:
- D
- DC
- ABDAB
- CBABCBA
In order to provide the Quiz Master with a potentially unlimited source of questions you are asked to write a program that will read input lines from standard input and will write to standard output.
输入描述
Each input line contains integers nn and LL (in that order), where n>0n>0 and LL is in the range 1≤L≤261≤L≤26. Input is terminated by a line containing two zeroes.
输出描述
For each input line prints out the nn-th hard sequence (composed of letters drawn from the first LL letters in the alphabet), in increasing alphabetical order (Alphabetical ordering here corresponds to the normal ordering encountered in a dictionary), followed (on the next line) by the length of that sequence. The first sequence in this ordering is ‘A’. You may assume that for given nn and LL there do exist at least nn hard sequences.
As such a sequence is potentially very long, split it into groups of four (4) characters separated by a space. If there are more than 16 such groups, please start a new line for the 17th group.
Your program may assume a maximum sequence length of 80.
For example, with L=3L=3, the first 7 hard sequences are:
A
AB
ABA
ABAC
ABACA
ABACAB
ABACABA
样例输入
7 3
30 3
0 0
样例输出
ABAC ABA
7
ABAC ABCA CBAB CABA CABC ACBA CABA
28
题目大意
输入一个n和L,由前L个字母组成的困难的串(相邻的没有相同的字串),按照字典序排序,输出第n个困难的串。
思路
递归深搜,加上判断,判断时间只判断后缀就行,前面部分已经判断满足条件,只需要判断后面即可。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5+10;
int n,l,cnt;
int a[N];
int dfs(int t)
{
if(cnt++ == n){//已经找到第n个
for(int i = 0;i < t;i++)//输出时间注意题目要求的格式
{
if(i%4 == 0 && i != 0 && i%64 != 0)
cout << " ";
else if(i != 0 && i%64 == 0)
cout << endl;
printf("%c", 'A'+a[i]);
}
if(t%64 != 0)
cout << endl;
cout << t << endl;
return 0;
}
for(int i = 0;i < l;i++){//深搜遍历
a[t] = i;
int flag = 1;
for(int j = 1;j*2 <= t+1;j++){//判断是否满足困难串的条件
int flag1 = 1;
for(int k = 0;k <j;k++){
if(a[t-k] != a[t-j-k]){
flag1 = 0;
break;
}
}
if(flag1)
{
flag = 0;
break;
}
}
/*
判断方法:从最后开始,最后一个与前面相邻的一个不相等就满足第一个,
其次进行间隔两个判断,依次进行,判断有无不满足的。
因为前面已经判断过了,所以每个只需要判一次。
*/
if(flag){
if(!dfs(t+1))//!dfs代表找到了返回了0
return 0;
}
}
return 1;
}
int main()
{
while(cin >> n >> l){
cnt = 0;
if(n == 0 && l == 0){
return 0;
}
dfs(0);
}
}
注意
特别注意:格式问题,题目中明确说明了,每4个为一组,一行最多16组,输出长度时间,如果正好最后有一个16组的换行就不需要换行,如果不是,需要输出一个换行。
边栏推荐
- Element positioning of Web Automation
- Realize reverse proxy client IP transparent transmission
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- One article deals with the microstructure and instructions of class
- Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
- Masked Autoencoders Are Scalable Vision Learners (MAE)
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- Composition of interface
- audiopolicy
猜你喜欢
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Masked Autoencoders Are Scalable Vision Learners (MAE)
Selenium+pytest automated test framework practice
Simple and beautiful method of PPT color matching
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
My experience and summary of the new Zhongtai model
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
Lesson 1: serpentine matrix
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Arduino 测量交流电流
随机推荐
513. Find the value in the lower left corner of the tree
Alibaba Tianchi SQL training camp task4 learning notes
Three. Js-01 getting started
透彻理解JVM类加载子系统
openresty ngx_lua正則錶達式
【Note17】PECI(Platform Environment Control Interface)
[untitled]
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
openresty ngx_ Lua request response
Hcip day 12 (BGP black hole, anti ring, configuration)
VOT Toolkit环境配置与使用
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Common JVM tools and optimization strategies
openresty ngx_lua正则表达式
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
查看网页最后修改时间方法以及原理简介
Finally understand what dynamic planning is
audiopolicy
Binary tree (II) -- code implementation of heap