1032 Sharing (25 point(s))

1032 Sharing (25 point(s))

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.

fig.jpg

Figure 1

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

Input Specification:

Each input file contains one test case. 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

whereAddress 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.

Output Specification:

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.

Sample Input 1:

11111 22222 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010

Sample Output 1:

67890

Sample Input 2:

00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

Sample Output 2:

-1

非常有趣的题。求公共链表。重点是采用nextElem[]记录后继结点的思维。

由于地址都是5位的,用1e5的数组记录它们的后继结点,这样就可以根据题中给出的开始结点,通过访问nextElem[]数组来得出每一条路径,可以用两个vector存储,这样向后回溯,直到找出第一个不相同的结点。

需要注意的是,有可能某个路径是另一个路径的子路径,即不存在第一个不相同的结点。

错误代码(case#5有WA,23分)

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX = 1e5+7;
int nextElem[MAX];
int main(void){
	int start1,start2;char c;int N;
	cin>>start1>>start2>>N;
	int front,rear;
	memset(nextElem,-1,sizeof(nextElem));
	while(N--){
		cin>>front>>c>>rear;
		nextElem[front] = rear;
	}
	vector<int> v1,v2;
	while(start1!=-1){
		v1.push_back(start1);
		start1 = nextElem[start1];
	}
	while(start2!=-1){
		v2.push_back(start2);
		start2 = nextElem[start2];
	}
	int i=v1.size()-1,j=v2.size()-1;
	if(i<0||j<0||v1[i]!=v2[j]){//注意
		printf("-1\n");
	}
	else{
		for(;i>=0&&j>=0&&v1[i];i--,j--){
			if(v1[i]!=v2[j]){
				printf("%05d\n",v1[i+1]);
				break;
			}
		}	
	}
	return 0;
}

AC代码:

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX = 1e5+7;
int nextElem[MAX];
int main(void){
	int start1,start2;char c;int N;
	cin>>start1>>start2>>N;
	int front,rear;
	memset(nextElem,-1,sizeof(nextElem));
	while(N--){
		cin>>front>>c>>rear;
		nextElem[front] = rear;
	}
	vector<int> v1,v2;
	while(start1!=-1){
		v1.push_back(start1);
		start1 = nextElem[start1];
	}
	while(start2!=-1){
		v2.push_back(start2);
		start2 = nextElem[start2];
	}
	int i=v1.size()-1,j=v2.size()-1;
	if(i<0||j<0||v1[i]!=v2[j]){
		printf("-1\n");
	}
	else{
		for(;i>=0&&j>=0&&v1[i]==v2[j];i--,j--);
		printf("%05d\n",v1[i+1]);
	}
	return 0;
}

其它思路:

1. 依然用vector形成路径,利用set的去重性质求集合交集,来计算有多少从哪个元素开始重复。

2. 根据第一个链表采用哈希表标记出现过的结点,遍历第二个链表时出现标记即可输出。

大神链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79573180

【CNN-GRU-Attention】基于卷积神经网络和门控循环单元网络结合注意力机制的多变量回归预测研究(Matlab代码实现)内容概要:本文介绍了基于卷积神经网络(CNN)、门控循环单元网络(GRU)与注意力机制(Attention)相结合的多变量回归预测模型研究,重点利用Matlab实现该深度学习模型的构建与仿真。该模型通过CNN提取输入数据的局部特征,利用GRU捕捉时间序列的长期依赖关系,并引入注意力机制增强关键时间步的权重,从而提升多变量时间序列回归预测的精度与鲁棒性。文中涵盖了模型架构设计、训练流程、参数调优及实际案例验证,适用于复杂非线性系统的预测任务。; 适合人群:具备一定机器学习与深度学习基础,熟悉Matlab编程环境,从事科研或工程应用的研究生、科研人员及算法工程师,尤其适合关注时间序列预测、能源预测、智能优化等方向的技术人员。; 使用场景及目标:①应用于风电功率预测、负荷预测、交通流量预测等多变量时间序列回归任务;②帮助读者掌握CNN-GRU-Attention混合模型的设计思路与Matlab实现方法;③为学术研究、毕业论文或项目开发提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码逐模块理解模型实现细节,重点关注数据预处理、网络结构搭建与注意力机制的嵌入方式,并通过调整超参数和更换数据集进行实验验证,以深化对模型性能影响因素的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值