浙江大学树的同构问题


第一步:需要解决scanf()函数数据输入的问题

scanf()函数:转载链接

众所周知,C语言中的scanf函数的作用是从标准输入设备(通常是键盘)读取输入值,并存储到参数列表中指针所指向的内存单元。下面从几个方面说一下一些稍微细节的东西。下面的实验都在vc6.0中通过。

1、scanf的返回值

scanf通常返回的是成功赋值(从标准输入设备赋值到参数列表所指定的内存区域)的数据项数,如果出错或是遇到end of file(注意,如果想从键盘输入EOF,在windows的DOS窗口用Ctrl+Z 或F6;在UNIX系统上,用CTRL+D。),则返回EOF,比如:

scanf("%d%d", &x, &y);

如果x和y都被成功读入,那么scanf的返回值就是2;
如果只有x被成功读入,返回值为1;
如果x和y都未被成功读入,返回值为0;
如果遇到错误或遇到end of file,返回值为EOF。

2、scanf的处理机制

scanf以删除的方式从缓冲区读入数据(来自标准输入设备的数据存储在缓冲区),也就是说,scanf从缓冲区读入一个数据项,该数据项在缓冲区中就被清除掉了。而如果scanf需要读取一个数据项,返现缓冲区当前是空的,那么程序就会在scanf代码处阻塞,等待用户输入,scanf函数接收到相应的数据项之后,在缓冲区中将这一数据项清除,scanf函数返回,程序继续执行。

3、scanf对不同类型输入的处理方式

首先,要清除一个概念:空白字符(white space)。一般,程序中所指的空白字符是指空格(space),回车(enter)和指标符(table)。

3.1 整数%d

对于整型数据的输入,也就是说"%d"类型的输入,scanf默认的分割符是所有的空白字符(空格,回车和指标符都行)。也就是说如果一个scanf函数中出现scanf("%d%d",&a,&b),那么用任何一个空白字符来分隔两个整数a,b的值,变量a,b都可以接收到正确的输入。另外,要注意的是,scanf对于数字输入,会忽略输入数据项前面的空白字符。下面是例1:

[cpp]  view plain  copy
  1. Code:  
  2. #include<stdio.h>  
  3. int main()  
  4. {  
  5.     int a,b;  
  6.     printf("Input the value of a and b:");  
  7.     while(scanf("%d%d",&a,&b)!=EOF)  
  8.     {  
  9.         printf("a=%d,b=%d\n",a,b);  
  10.         printf("Input the value of a and b:");  
  11.     }  
  12.     return 0;  
  13. }  
  14. Output:  
  15. Input the value of a and b:123 456  
  16. a=123,b=456  
  17. Input the value of a and b:123  456  
  18. a=123,b=456  
  19. Input the value of a and b:123  
  20. 456  
  21. a=123,b=456  
  22. Input the value of a and b:  
  23. 123 456  
  24. a=123,b=456  
  25. Input the value of a and b:     123 456  
  26. a=123,b=456  
  27. Input the value of a and b: 123 456  
  28. a=123,b=456  
  29. Input the value of a and b:^Z  
  30. Press any key to continue  
3.2 字符串%s

scanf对于字符串输入的处理和对整数类似,会忽略前导的空白字符,而且默认的分隔符是所有的空白字符。但是,要注意的是,由于C语言中,没有string类型,都是用char型数组来表示。因此,scanf会为每一个输入的字符串最后加一个‘\0’。下面是一个例子,可以看出scanf这货的边界控制还是要小心。如下例2。

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4.     char a[5],b[5];  
  5.     int i;  
  6.     printf("Input the value of a and b:");  
  7.     while(scanf("%s%s",a,b)!=EOF)  
  8.     {  
  9.         printf("a=%s,b=%s\n",a,b);  
  10.         for(i=0;i<5;i++)  
  11.             printf("%d:(%c) ",a[i],a[i]);  
  12.         printf("\n");  
  13.         for(i=0;i<5;i++)  
  14.             printf("%d:(%c) ",b[i],b[i]);  
  15.         printf("\n");  
  16.         printf("Input the value of a and b:");  
  17.     }  
  18.     return 0;  
  19. }  
运行结果:


3.3 字符%c

scanf在处理对字符数据的输入时,既不会忽略前导空白字符,默认也没有任何分隔字符。所有的字符,包括空白字符都会被当成输入字符。下面是例3。

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4.     char a ,b ;  
  5.     printf("Input the value of a and b:");  
  6.     while(scanf("%c%c",&a,&b)!=EOF)  
  7.     {  
  8.         printf("a=%c,b=%c\n",a,b);  
  9.         printf("Input the value of a and b:");  
  10.     }  
  11.     return 0;  
  12. }  
运行结果:


可以看出,在对字符数据输入的时候,由于缓冲区中有回车空格等数据,会导致输入数据比较诡异,为了解决这个问题,有以下方法:

(1) 清空缓冲区

在微软系统中,有一个名为fflush(stdin)的函数,可以用来清空缓冲区,如下例4。

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4.     char a ,b ;  
  5.     printf("Input the value of a and b:");  
  6.     while(scanf("%c%c",&a,&b)!=EOF)  
  7.     {  
  8.         printf("a=%c,b=%c\n",a,b);  
  9.         fflush(stdin);  
  10.         printf("Input the value of a and b:");  
  11.     }  
  12.     return 0;  
  13. }  
运行结果:


(2)将缓冲区的数据读出来

有的编译系统并没有定义stdin的fflush操作,这个时候,可以把缓冲区中的数据读出来,有如下几种可行的方法:

1) getchar()

将例4中的fflush(stdin);语句换成

char c;
while((c=getchar())!='\n'&&c!=EOF);

运行效果和上面的相同。

2)gets()

char* gets(char* buffer)从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。读入成功,返回与参数buffer相同的指针;读入过程中遇到EOF(End-of-File)或发生错误,返回NULL指针。所以在遇到返回值为NULL的情况,要用ferror或feof函数检查是发生错误还是遇到EOF。
要注意的是gets函数可以无限读取,不会判断上限,所以应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。如果溢出,多出来的字符将被写入到堆栈中,这就覆盖了堆栈原先的内容,破坏一个或多个不相关变量的值。

将例4中的fflush(stdin);语句换成

char c[10];
gets(c);

运行效果也和上面的相同。

4、在stackoverflow上看到的一个问题

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. char *method1(void)  
  5. {  
  6.     static char a[4];  
  7.     scanf ("%s\n", a);  
  8.     return a;  
  9. }  
  10.   
  11. int main(void)  
  12. {  
  13.     char *h = method1();  
  14.     printf ("%s\n", h);  
  15.     return 0;  
  16. }  
运行结果:

[cpp]  view plain  copy
  1. ab  
  2. cd  
  3. ab  
  4. Press any key to continue  
可以发现,输如两次之后才会输出。这个现象比较诡异,原因如下:

White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input.  Everything else matches only itself.
Thus with scanf ("%s\n", a) it will scan for a string followed by optional white space. Since after the first newline more whitespace may follow, scanf is not done after the first newline and looks what's next. You will notice that you can enter any number of newlines (or tabs or spaces) and scanf will still wait for more.

However, when you enter the second string, the sequence of whitespace is delimited and scanning stops.


第二步:解决题目

题目:

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。


图1


图2

现给定两棵树,请你判断它们是否是同构的。

输入格式:

输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数NN (\le 1010),即该树的结点数(此时假设结点从0到N-1N1编号);随后NN行,第ii行对应编号第ii个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。

输出格式:

如果两棵树是同构的,输出“Yes”,否则输出“No”。

输入样例1(对应图1):

8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -

输出样例1:

Yes

输入样例2(对应图2):

8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4

输出样例2:

No

代码:

#include<iostream>
#include<cstdio>
using namespace std;


#define MaxTree 10
#define ElementType char
#define Tree int 
#define Null -1

struct TreeNode
{
	ElementType Element;
	Tree Left;
	Tree Right;
}T1[MaxTree],T2[MaxTree];


Tree BuildTree(struct TreeNode T[]){
	
	int i,N,Root = Null;
	char cl, cr;
	
	int check[MaxTree];
	
	scanf("%d",&N);
	getchar();
	
	if(N){
		
		for(i = 0;i<N;i++) check[i] = 0;
		
		for(i = 0;i<N;i++){
			scanf("%c %c %c",&T[i].Element,&cl,&cr);
			getchar();
			if(cl != '-'){
				T[i].Left  = cl-'0';
				check[T[i].Left] = 1;
			}
			else {
				T[i].Left = Null;
			}
			
			if (cr != '-'){
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else
			{
				T[i].Right = Null;
			}
		}
		for ( i = 0; i < N; i++)
		{
			if (!check[i]){
				Root = i;
				break;	
			}	
		}
	}
	return Root;
}

int Isomorphic(Tree R1, Tree R2)
{
	if ((R1 == Null) && (R2 == Null))
		return 1;

	if (((R1 == Null) && (R2 != Null)) || ((R1 != Null) && (R2 == Null)))
		return 0;

	if (T1[R1].Element != T2[R2].Element)
		return 0;

	if ((T1[R1].Left == Null) && (T2[R2].Left == Null))
		return Isomorphic(T1[R1].Right, T2[R2].Right);

	if (((T1[R1].Left != Null) && (T2[R2].Left != Null)) && ((T1[T1[R1].Left].Element) == (T2[T2[R2].Left].Element)))
		return (Isomorphic(T1[R1].Left, T2[R2].Left) && Isomorphic(T1[R1].Right, T2[R2].Right));
	else
		return (Isomorphic(T1[R1].Left, T2[R2].Right) && Isomorphic(T1[R1].Right, T2[R2].Left));
}

int main(){
	Tree R1;
	Tree R2;
	R1 = BuildTree(T1);
	R2 = BuildTree(T2);
	if(Isomorphic(R1,R2)) printf("Yes\n");
	else printf("No\n");
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/qq_34686440/article/details/80358119