129 - Krypton Factor
来源:互联网 发布:一本书学会做数据分析 编辑:程序博客网 时间:2024/05/24 15:37
You have beenemployed by the organisers of a Super Krypton Factor Contest in whichcontestants have very high mental and physical abilities. In one section of thecontest the contestants are tested on their ability to recall a sequence ofcharacters which has been read to them by the Quiz Master. Many of thecontestants are very good at recognising patterns. Therefore, in order to addsome difficulty to this test, the organisers have decided that sequencescontaining certain types of repeated subsequences should not be used. However,they do not wish to remove all subsequences that are repeated, since in thatcase no single character could be repeated. This in itself would make theproblem too easy for the contestants. Instead it is decided to eliminate allsequences containing an occurrence of two adjoining identical subsequences.Sequences containing such an occurrence will be called ``easy''. Othersequences will be called ``hard''.
For example, thesequence ABACBCBAD is easy, since it contains an adjoining repetition of thesubsequence CB. Other examples of easy sequences are:
- BB
- ABCDACABCAB
- ABCDABCD
Some examples ofhard sequences are:
- D
- DC
- ABDAB
- CBABCBA
Input and Output
In order toprovide the Quiz Master with a potentially unlimited source of questions youare asked to write a program that will read input lines that containintegers n and L (in that order), where n >0 and L is in the range , andfor each input line prints out the nth hard sequence (composed ofletters drawn from the first L letters in the alphabet), inincreasing alphabetical order (alphabetical ordering here corresponds to thenormal ordering encountered in a dictionary), followed (on the next line) bythe length of that sequence. The first sequence in this ordering is A. You mayassume that for given n and L there do existat least n hard sequences.
For example,with L = 3, the first 7 hard sequences are:
A
AB
ABA
ABAC
ABACA
ABACAB
ABACABA
As each sequenceis potentially very long, split it into groups of four (4) characters separatedby a space. If there are more than 16 such groups, please start a new line forthe 17th group.
Therefore, if theintegers 7 and 3 appear on an input line, the output lines produced should be
ABAC ABA
7
Input isterminated by a line containing two zeroes. Your program may assume a maximumsequence length of 80.
Sample Input
30 3
0 0
Sample Output
ABAC ABCA CBABCABA CABC ACBA CABA
28
代码:
#include<iostream>
#include<string>
#include<cstring>
using namespacestd;
int n,l,num;
char ans[100];
bool dfs(int cur);
int main()
{
while(cin>>n>>l&&n&&l)
{
memset(ans,0,sizeof(ans));
num=0;
dfs(0);
}
return 0;
}
bool dfs(int cur)
{
if(num==n)
{
string s=ans;//转换成字符串操作简便
int kase=0;
for(int i=0; i<s.size(); i=i+4)
{
if(kase&&kase%16==0)
{
cout<<endl;
}
if(kase%16==0)
{
cout<<s.substr(i,4);
kase++;
}
else
{
cout<<""<<s.substr(i,4);
kase++;
}
}
cout<<endl<<s.size()<<endl;
return true;//找到答案
}
else
{
for(int i=0;i<l;i++)
{
if(cur==0||(cur&&ans[cur-1]!=char(i+'A')))
/*
不能将A[0]指定为’A’,也可以以’B’开头;不要取与当前字符串最后一个字符相同的字符
*/
{
ans[cur]=char(i+'A');
int ok=1;
string s=ans;
/*
每次只需要判断新添加的字符所参与构成的字串,而无需将当前新串的所有子串(包括不含新添加的字符的子串,无需判断,就像八皇后问题中:只需要判断当前皇后与以前的皇后是否冲突,但并不需要判断以前的皇后是否互相冲突
*/
for(intj=1;j*2<=s.size();j++))
{
if(s.substr(cur-j+1,j)==s.substr(cur-2*j+1,j))
{
ok=0;//只要存在相邻相同的子串
break;
}
}
if(ok)//若添加字符后满足要求
{
num++;//数量+1
if(dfs(cur+1))
//递归搜素:如果已经找到解(返回值为true),则直接退出,不需要再回溯
{
return true;
}
ans[cur]=0;
//十分重要!!!没找到结果,向上一级回溯的时候:一定将添加的字符清空
}
else
{
ans[cur]=0;
}
}
}
return false;
}
}
- uva 129 Krypton Factor
- uva 129 - Krypton Factor
- UVa 129 Krypton Factor
- UVA - 129 Krypton Factor
- 129 - Krypton Factor
- UVa-129 - Krypton Factor
- UVa - 129 - Krypton Factor
- UVA - 129 Krypton Factor
- uva 129 - Krypton Factor
- uva 129 - Krypton Factor
- UVA 129 Krypton Factor
- UVA - 129 Krypton Factor
- 129 - Krypton Factor
- UVa 129 Krypton Factor
- uva 129 Krypton Factor
- UVA 129 - Krypton Factor
- uva 129 Krypton Factor
- UVa-129-Krypton Factor
- c#数组倒序存放
- 用unity 编写程序,把由10个元素组成的一维数组逆序存放再输出。
- WM_NCCALCSIZE消息处理详解
- UVa508 - Morse Mismatches
- Rails与数据库查询到关于datetime的记录不一致引发的讨论
- 129 - Krypton Factor
- java类初始化顺序
- 使用Wininet发送短信
- hibernate学习:映射之主键
- 140 - Bandwidth
- UVA 11154 Weights and Measures(线性DP)
- 524 - Prime Ring Problem
- 725 - Division
- 2015年如何利用网络赚钱