洛谷 P1087 :FBI树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41505957/article/details/84590794

https://www.luogu.org/problemnew/show/P1087

 

输入输出样例

输入样例#1: 复制

3
10001011

输出样例#1: 复制

IBFBBBFIBFIIIFF

在将b[j]置0点时候错了几次。

后序遍历序列就是先左后右再访问根节点

找出规律,从最小的也就是长度为1的节点开始访问。

每访问两个相同长度的节点,就访问他们的根节点。

#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 2020
char str[N];
int a[20],b[20];
int main()
{
	int n,m,i,j;
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		scanf("%s",str);
		m=pow(2,n);
		for(i=0;i<m;i++)
		{
			if(str[i]=='0')
			{
				printf("B");
				if(!a[1])
					b[1]=1;
			}						
			else
			{
				printf("I");
				if(!a[1])
					b[1]=2;
			}
				
			a[1]++;	
			for(j=1;j<=n+2;j++)
			{
				if(a[j]==2)
				{
					a[j]=0;
					a[j+1]++;
					if(str[i]=='0'&&b[j]==1)
					{
						printf("B");
						if(b[j+1]==0||b[j+1]==1)
							b[j+1]=1;
						else
							b[j+1]=3;
					}			
					else if(str[i]=='1'&&b[j]==2)
					{
						printf("I");
						if(b[j+1]==2||b[j+1]==0)
							b[j+1]=2;
						else
							b[j+1]=3;
					}	
					else
					{
						printf("F");
						b[j+1]=3;
					}
					b[j]=0;		  //重要
				}
			}	
		}
		printf("\n");
	}
	return 0;
}
扫描二维码关注公众号,回复: 4295997 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_41505957/article/details/84590794