求助清华OJ题目

weixin_45777984 2020-05-13 11:02:51
自己设计所有样例都没有问题,但放在OJ上就都通过不了,想求助大佬

题目描述
SPOC2020 3-4 Diligence

描述
  邓老师的 mooc 有许多同学选修,这其中有很多外国的同学,于是他打开刷题记录,想找出最认真的那位同学。

  刷题提交记录共有 n 条,每条记录包含一个人名,所有同学的名字各不相同。

  邓老师认为刷题记录中出现最多的人就是最认真的那位同学。于是他开始查找,虽然邓老师脑容量很大,但是他不想花很多在这种单调的工作上,于是他只分配了很少的脑容量来完成这项任务。

  请你设计一个算法,帮助邓老师找到最认真的人。

输入
  第一行仅含一个整数,即记录的总数 n。

  接下来的 n 行给出记录,每行各用一个字符串给出一个人名。

输出
  仅一行,包含一个字符串(最认真的人的名字)和一个整数(对应的刷题提交次数)。

输入样例
10
bales
gazier
peterson
jones
watts
peterson
bales
wales
peterson
jones

输出样例
peterson 3

数据范围
  1 <= n <= 2*10^6

  同学总数 <= 10^4

  所有名字均由小写字母组成,且长度不超过 8 个字符(不含换行符)。

  最认真的人保证是唯一的。

资源限制
  时间:0.7 sec

  空间:20 MB

  *即使提交一个只包含空的 main 函数的程序,也会占用一定空间,这部分空间已经考虑在内。

  *为了使空间计量更公平,本题编译流程做了特殊设置,无论提交 .c 还是 .cpp、是否使用 C++ 标准库,都会链接 libstdc++。
...全文
368 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_45777984 2020-05-13
  • 打赏
  • 举报
回复
使用的是AVL树进行储存,为了保持平衡,使用了3+4重构。自己有设计几百的样例都没有问题,但一旦交到OJ平台上就不行了,感谢各位大佬orzzzzz
weixin_45777984 2020-05-13
  • 打赏
  • 举报
回复
抱歉不太会操作,加了一些注释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;
}
自信男孩 2020-05-13
  • 打赏
  • 举报
回复
不是很难的问题,自己先尝试写写,不明白的贴出代码,帮你解决一下~

70,018

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧