HYSBZ - 3033[太鼓达人] 题解&代码

Description

七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行。这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk、Poet_shy和lydrainbowcat拯救出来的的applepi。看到两人对太鼓达人产生了兴趣,applepi果断闪人,于是cl拿起鼓棒准备挑战。然而即使是在普通难度下,cl的路人本性也充分地暴露了出来。一曲终了,不但没有过关,就连鼓都不灵了。Vani十分过意不去,决定帮助工作人员修鼓。

鼓的主要元件是M个围成一圈的传感器。每个传感器都有开和关两种工作状态,分别用1和0表示。显然,从不同的位置出发沿顺时针方向连续检查K个传感器可以得到M个长度为K的01串。Vani知道这M个01串应该是互不相同的。而且鼓的设计很精密,M会取到可能的最大值。现在Vani已经了解到了K的值,他希望你求出M的值,并给出字典序最小的传感器排布方案。

Input

一个整数K。(2≤K≤11)

Output

一个整数M和一个二进制串,由一个空格分隔。表示可能的最大的M,以及字典序最小的排布方案,字符0表示关,1表示开。你输出的串的第一个字和最后一个字是相邻的。

Sample Input

3

Sample Output

8 00010111

分析:

先求第一问,手动模拟一下,我们发现:

  • 当k=1时 M的长度为2
  • 当k=2时 M的长度为4
  • 当k=3时 M的长度为8
……

便可以大胆猜测,M的长度为 2 k 2^{k} 2k
发现正确。。。。
第一问解决了,大快人心,再来第二问。
k<=11,这说明了M的最大长度不超过 2 11 2^{11} 211,dfs暴力枚举01位根本不慌,那就dfs吧。
因为从M中每一个位置向后走k个得到的M个k位01串要互不相同,我们就开一个map,记录当前串中的k位的01串出现情况。dfs中,如果发现这个子串出现过,就说明这种方法01填写不行,return;如果没有出现过就标记为出现过,然后继续向下一位dfs。最后输出就可以了。

代码如下:
#include 
using namespace std;
map < string , bool >bo;
string s,s1;
int sum=0,k;
void dfs(string s1,int ans)
{
	if (ans==sum && bo[s1]==0)
	{
		string s2=s1.substr(0,sum);
		cout<<s2;
		exit(0);
	}
	string s2=s1.substr(ans,sum-1);
	if (!bo[s2+'0'])
	{
		bo[s2+'0']=1;
		dfs(s1+'0',ans+1);
		bo[s2+'0']=0;
	}
	if (!bo[s2+'1'])
	{
		bo[s2+'1']=1;
		dfs(s1+'1',ans+1);
		bo[s2+'1']=0;
	}
}
int main()
{
	int n,m,k;
	scanf("%d",&k);
	sum=(1<<k);
	printf("%d ",sum);
	
	//打表代码,请不要在意
	
//	if (k==1) printf("01");
//	if (k==2) printf("0011");
//	if (k==3) printf("00010111");
//	if (k==4) printf("0000100110101111");
//	if (k==5) printf("00000100011001010011101011011111");
//	if (k==6) printf("0000001000011000101000111001001011001101001111010101110110111111");
//	if (k==7) printf("00000001000001100001010000111000100100010110001101000111100100110010101001011100110110011101001111101010110101111011011101111111");
//	if (k==8) printf("0000000010000001100000101000001110000100100001011000011010000111100010001001100010101000101110001100100011011000111010001111100100101001001110010101100101101001011110011001101010011011100111011001111010011111101010101110101101101011111011011110111011111111");
//	if (k==9) printf("00000000010000000110000001010000001110000010010000010110000011010000011110000100010000100110000101010000101110000110010000110110000111010000111110001000110001001010001001110001010010001010110001011010001011110001100110001101010001101110001110010001110110001111010001111110010010010110010011010010011110010100110010101010010101110010110110010111010010111110011001110011010110011011010011011110011101010011101110011110110011111010011111110101010110101011110101101110101110110101111110110110111110111011110111111111");
//	if (k==10) printf("0000000000100000000110000000101000000011100000010010000001011000000110100000011110000010001000001001100000101010000010111000001100100000110110000011101000001111100001000010001100001001010000100111000010100100001010110000101101000010111100001100010000110011000011010100001101110000111001000011101100001111010000111111000100010100010001110001001001000100101100010011010001001111000101001100010101010001010111000101100100010110110001011101000101111100011000110010100011001110001101001000110101100011011010001101111000111001100011101010001110111000111100100011110110001111101000111111100100100110010010101001001011100100110110010011101001001111100101001010011100101010110010101101001010111100101100110010110101001011011100101110110010111101001011111100110011010011001111001101010100110101110011011011001101110100110111110011100111010110011101101001110111100111101010011110111001111101100111111010011111111010101010111010101101101010111110101101011011110101110111010111101101011111110110110111011011111101110111110111101111111111");
//	if (k==11) printf("00000000000100000000011000000001010000000011100000001001000000010110000000110100000001111000000100010000001001100000010101000000101110000001100100000011011000000111010000001111100000100001000001000110000010010100000100111000001010010000010101100000101101000001011110000011000100000110011000001101010000011011100000111001000001110110000011110100000111111000010000110000100010100001000111000010010010000100101100001001101000010011110000101000100001010011000010101010000101011100001011001000010110110000101110100001011111000011000110000110010100001100111000011010010000110101100001101101000011011110000111000100001110011000011101010000111011100001111001000011110110000111110100001111111000100010010001000101100010001101000100011110001001001100010010101000100101110001001100100010011011000100111010001001111100010100011000101001010001010011100010101001000101010110001010110100010101111000101100110001011010100010110111000101110010001011101100010111101000101111110001100011100011001001000110010110001100110100011001111000110100110001101010100011010111000110110010001101101100011011101000110111110001110010100011100111000111010010001110101100011101101000111011110001111001100011110101000111101110001111100100011111011000111111010001111111100100100101001001001110010010101100100101101001001011110010011001100100110101001001101110010011101100100111101001001111110010100101100101001101001010011110010101001100101010101001010101110010101101100101011101001010111110010110011100101101011001011011010010110111100101110011001011101010010111011100101111011001011111010010111111100110011011001100111010011001111100110100111001101010110011010110100110101111001101101010011011011100110111011001101111010011011111100111001111001110101010011101011100111011011001110111010011101111100111101011001111011010011110111100111110101001111101110011111101100111111101001111111110101010101101010101111010101101110101011101101010111111010110101110101101101101011011111010111011110101111011101011111011010111111110110110111101101110111011011111110111011111101111011111011111111111");
	for (int i=1;i<=k;i++) s1+='0';
	bo[s1]=1;
	dfs(s1,1);
//	cout<
	return 0;
}

你可能感兴趣的:(题解)