当前位置:网站首页>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组的换行就不需要换行,如果不是,需要输出一个换行。
边栏推荐
- Fix the memory structure of JVM in one article
- Boring boring
- Ieventsystemhandler event interface
- How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
- Three.js-01 入门
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
- One article deals with the microstructure and instructions of class
- Negative sampling
- 分布式解决方案选型
猜你喜欢

Using LNMP to build WordPress sites

一文搞定垃圾回收器

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~

openresty ngx_ Lua request response
![[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]](/img/b4/af689abb3ad4e25988f2d17152406e.jpg)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]

Three.js-01 入门

一文搞定JVM常见工具和优化策略

Element positioning of Web Automation

Starting from 1.5, build a micro Service Framework -- log tracking traceid

Usage Summary of scriptable object in unity
随机推荐
Error when LabVIEW opens Ni instance finder
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Douban scoring applet Part-2
Marginal probability and conditional probability
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
[screen recording] how to record in the OBS area
透彻理解JVM类加载子系统
Nanjing: full use of electronic contracts for commercial housing sales
C Primer Plus Chapter 9 question 10 binary conversion
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
分布式解决方案选型
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
fibonacci search
Selenium+pytest automated test framework practice
我把开源项目alinesno-cloud-service关闭了
TCC of distributed solutions
openresty ngx_lua正则表达式
Record several frequently asked questions (202207)