浙江大学复试编程题之——Sharing

题目描述:

To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are stored as showed in Figure 1. 

Figure 1 You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1). 


输入描述:

For each case, the first line contains two addresses of nodes and a positive N (<= 10^5), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and Next is the position of the next node.

输出描述:

For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output "-1" instead.
(题目转自牛客网)


===================================================================================================================

意译:题目给我们两个字符串,为了节省空间,两个字符串有可能后面有交汇的结点,比如:being与playing可以公用ing来节省空间,而结点i就是他             们的第一个交汇结点,现在给我们不超过10万个结点,共可以组成两个字符串,每个结点有下一个结点的地址,与本结点代表字母与本结点地址           的信息。要求我们找出两个字符串中第一个交汇的结点的地址并显示。

输入描述:

      第一个串头的地址  第二个串头的地址 结点个数N

                      第一个结点地址 第一个结点内含字母 相对于第一个结点所在串中位置的下一位置的结点

      第二个结点地址 第二个结点内含字母 相对于第二个结点所在串中位置的下一位置的结点

             。。。。。。

                             第N个结点地址 第N个结点内含字母 相对于第N个结点所在串中位置的下一位置的结点


输出描述:

若有交汇结点则输出第一个交汇的结点的地址,若无则输出-1


====================================================================================================================================

这题就是要求求解两个链表第一个重合的结点,本来想把地址和索引分开的,但是很可惜复杂度会变成n^2会超时,所以只好牺牲空间直接用地址值作为索引值了。算法分为几步:
一、将所有结点的flag置0.输入所有结点信息,并记录两个串头所在位置。
二、从第一个串头开始遍历,没经过一个结点则该结点的flag+1.
三、开始遍历第二个串,规则如上,但是多了一个条件判断:若当前结点的flag加1会变成2则返回当前结点的地址(此结点即是第一个遍历到的重合结点)。
四、输出
====================================================================================================================================

#include<iostream>
#include<vector>

using namespace std;
typedef struct node{
	char data;
	int Next_Addr;               //下一个结点地址 
	int flag;                    //访问次数标记 
}Node;

int Get_Com(vector<Node> &Pre, int h1, int h2){
	int i,t1,t2;          
	i = h1;                        
	while(i != -1){
		Pre[i].flag += 1;
		i = Pre[i].Next_Addr;     //访问串1的下一个结点 
	}
	i = h2;
	while(i != -1){
		Pre[i].flag += 1;
		if(Pre[i].flag == 2)
			return i;
		i = Pre[i].Next_Addr;
	}
	return -1;                   //说明所有结点都只有被访问过一次,即无交汇结点,返回-1 
}

int main(){
	int N,Addr1,Addr2,temp_Addr;
	int Com;
	vector<Node> Pre;
	while(cin>>Addr1>>Addr2>>N){
		Pre.clear();
		Pre.resize(100000);
		for(int i=0; i<N; i++)
			Pre[i].flag = 0;
		for(int i=0; i<N; i++){
			cin>>temp_Addr;
			cin>>Pre[temp_Addr].data>>Pre[temp_Addr].Next_Addr;
		}
		Com = Get_Com(Pre,Addr1,Addr2);
		if(Com != -1){                   //-1前面不需要补充0 
			cout.width(5);
			cout.fill('0');
		}
		cout<<Com<<endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值