user-0 提问: 求助清华OJ题目

回复:
user: user-0
content: 使用的是AVL树进行储存，为了保持平衡，使用了3+4重构。自己有设计几百的样例都没有问题，但一旦交到OJ平台上就不行了，感谢各位大佬orzzzzz

user: user-0
content: 抱歉不太会操作，加了一些注释orz（没有养成很好的变成习惯  #include <stdio.h> #include <string.h> #include <stdlib.h>  struct stu{ 	char name[10]; 	int times;  	int par; 	int lc; 	int rc; 	int BalFac; 	int dep; }record[11000],*max;  int Root = 0;  int Search(int root, char x[10]); void Insert(int root, int x); int SearchIns(int root, char x[10]); int CalDep(int x); int CalBal(int x); int Reconnect(int a, int b, int c, int t0, int t1, int t2, int t3); void Rebalance(int x);  int Max(int a, int b);  int main() { 	unsigned n,i; 	unsigned pos = 0; 	char temp[10]; 	int res; 	 	scanf ("%d", &n); 	getchar();  	for (i=0; i<n; i++) 	{ 		gets (temp); 		 		//If it's the first element, mark it as the root 		if (i==0) 		{ 			strcpy (record[0].name,temp); 			record[0].times ++; 			pos++; 			max = &record[0];  			record[0].par = -1; 			record[0].lc = -1; 			record[0].rc = -1; 		}  		//If it's not, insert it into the existing tree 		else 		{ 			res=Search(Root, temp); 			 			//Already exists 			if (res != -1) 			{ 				record[res].times++; 				if (record[res].times  >  max->times) 				{ 					max = &record[res]; 				} 			} 			 			//Haven't been inserted before 			else 			{ 				strcpy (record[pos].name, temp); 				record[pos].times = 1; 				if (record[pos].times  >  max->times) 				{ 					max = &record[pos]; 				} 				//Insert into the tree 				Insert(Root, pos);  				pos++; 			} 		}  	}   	for (i=0; i<=strlen(max->name); i++) 	{ 		printf("%c", max->name[i]); 	} 	printf("%d\n", max->times);  	system ("pause");  }  //Insert the element into the tree void Insert(int root, int x) { 	int Par; 	int unbal; 	int rebal = 0;  	Par = SearchIns(root, record[x].name); 	unbal = Par;  	if (strcmp(record[Par].name, record[x].name) == 0) 	{ 		record[Par].rc = x; 		record[x].par = Par; 		record[x].lc = -1; 		record[x].rc = -1; 		 		record[Par].dep = CalDep(Par); 		record[Par].BalFac = CalBal(Par); 	}  	else if (strcmp(record[Par].name, record[x].name) < 0) 	{ 		record[Par].rc = x; 		record[x].par = Par; 		record[x].lc = -1; 		record[x].rc = -1; 		 		record[Par].dep = CalDep(Par); 		record[Par].BalFac = CalBal(Par); 	}  	else 	{ 		record[Par].lc = x; 		record[x].par = Par; 		record[x].lc = -1; 		record[x].rc = -1; 		 		record[Par].dep = CalDep(Par); 		record[Par].BalFac = CalBal(Par); 	}   	while(unbal != root) 	{ 		unbal = record[unbal].par; 		record[unbal].dep = CalDep(unbal); 		record[unbal].BalFac = CalBal(unbal); 		if (rebal != 1 && (record[unbal].BalFac > 1 || record[unbal].BalFac < -1) ) 		{ 			rebal = 1; 		} 	}  	if (rebal == 1) 	{ 		Rebalance(unbal); 		rebal = 0; 	} 		 }  //Search for the element used in judging whether it exists. If it doesn't exist, return -1 int Search(int root, char x[10]) { 	if (strcmp(record[root].name, x) == 0) 	{ 		return root; 	}  	else if ( strcmp(record[root].name, x)<0 ) 	{ 		if (record[root].rc != -1) 		{ 			return Search(record[root].rc, x); 		} 		else 		{ 			return -1; 		} 	}  	else 	{ 		if (record[root].lc != -1) 		{ 			return Search(record[root].lc, x); 		} 		else 		{ 			return -1; 		} 	} }   //Search for the element used in the insert funciont. If it doesn't exist, return the parent int SearchIns(int root, char x[10]) { 	if (strcmp(record[root].name, x) == 0) 	{ 		if (record[root].lc == -1 || record[root].rc == -1) 		{ 			return root; 		} 		 		else 		{ 			return SearchIns(record[root].rc, x); 		} 	}  	else if ( strcmp(record[root].name, x)<0 ) 	{ 		if (record[root].rc != -1) 		{ 			return SearchIns(record[root].rc, x); 		} 		else 		{ 			return root; 		} 	}  	else 	{ 		if (record[root].lc != -1) 		{ 			return SearchIns(record[root].lc, x); 		} 		else 		{ 			return root; 		} 	} }  //Calculate the depth int CalDep(int x) { 	int max;  	if (record[x].lc == -1) 	{ 		if (record[x].rc == -1) 		{ 			return 0; 		} 		else 		{ 			return record[record[x].rc].dep +1 ; 		} 	}  	else 	{ 		if (record[x].rc == -1) 		{ 			return record[record[x].lc].dep + 1; 		} 		else 		{ 			max = Max(record[record[x].rc].dep, record[record[x].lc].dep); 			return max + 1; 		} 	} }  //Calculate the balance factor of AVL tree int CalBal(int x) { 	int res; 	 	if (record[x].lc == -1) 	{ 		if (record[x].rc == -1) 		{ 			return 0; 		} 		else 		{ 			return -record[record[x].rc].dep-1 ; 		} 	}  	else 	{ 		if (record[x].rc == -1) 		{ 			return record[record[x].lc].dep+1; 		} 		else 		{ 			res = record[record[x].lc].dep - record[record[x].lc].dep; 			return res; 		} 	}  }  //Find the bigger  int Max(int a, int b) { 	if (a>b) 	{ 		return b; 	} 	else 	{ 		return a; 	} }  //3+4 重构，找到最长子链 void Rebalance(int x) { 	int p; 	int v; 	int NewRoot; 	struct stu G, P, V;  	G = record[x]; 	if (G.lc == -1 || record[G.rc].dep > record[G.lc].dep ) 	{ 		p = G.rc; 	} 	else 	{ 		p = G.lc; 	} 	P = record[p];  	if (P.lc == -1 || record[P.rc].dep > record[P.lc].dep ) 	{ 		v = P.rc; 	}  	else 	{ 		v = P.lc ; 	} 	V = record[v];  	if (record[P.par].lc == p) 	{ 		if (record[V.par].lc == v) 		{ 			P.par = G. par; 			record[p] = P; 			NewRoot = Reconnect (v, p, x, V.lc, V.rc, P.rc, G.rc); 			 			record[x].dep = CalDep(x); 			record[x].BalFac = CalBal(x);  			record[v].dep = CalDep(v); 			record[v].BalFac = CalBal(v);  			record[p].dep = CalDep(p); 			record[p].BalFac = CalBal(p);   		} 		else 		{ 			V.par = G.par; 			record[v] = V; 			NewRoot = Reconnect (p, v, x, P.lc, V.lc, V.rc, G.rc); 			 			record[x].dep = CalDep(x); 			record[x].BalFac = CalBal(x);  			record[p].dep = CalDep(p); 			record[p].BalFac = CalBal(p);  			record[v].dep = CalDep(v); 			record[v].BalFac = CalBal(v); 		} 	}  	else 	{ 		if (record[V.par].lc == v ) 		{ 			V.par = G.par; 			record[v] = V; 			NewRoot = Reconnect (x, v, p, G.lc, V.lc, V.rc, P.rc); 			 			record[x].dep = CalDep(x); 			record[x].BalFac = CalBal(x);  			record[p].dep = CalDep(p); 			record[p].BalFac = CalBal(p);  			record[v].dep = CalDep(v); 			record[v].BalFac = CalBal(v);  		} 		else 		{ 			P.par = G.par; 			record[p] = P; 			NewRoot = Reconnect (x, p, v, G.lc, P.lc, V.lc, V.rc); 			 			record[x].dep = CalDep(x); 			record[x].BalFac = CalBal(x);  			record[v].dep = CalDep(v); 			record[v].BalFac = CalBal(v);  			record[p].dep = CalDep(p); 			record[p].BalFac = CalBal(p);  		} 	}  	if (x == Root) 	{ 		Root = NewRoot; 	}  }  //3+4 重构，重新连接 int Reconnect(int a, int b, int c, int t0, int t1, int t2, int t3) { 	record[a].lc = t0; 	if (t0 != -1) 	{ 		record[t0].par = a; 	}  	record[a].rc = t1; 	if (t1 != -1) 	{ 		record[t1].par = a; 	}  	record[c].lc = t2; 	if (t2 != -1) 	{ 		record[t2].par = c; 	}  	record[c].rc = t3; 	if (t3 != -1) 	{ 		record[t3].par = c; 	}  	record[b].lc = a; 	record[a].par = b; 	record[b].rc = c; 	record[c].par = b;  	return b; }

user: user-1
content: 不是很难的问题，自己先尝试写写，不明白的贴出代码，帮你解决一下～