题记(26)--Sharing(链表公共后缀)

2024-01-24 00:44

本文主要是介绍题记(26)--Sharing(链表公共后缀),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、题目内容

二、输入描述

三、输出描述

四、输入输出示例

五、完整C语言代码


一、题目内容

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.

二、输入描述

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.

四、输入输出示例

输入:

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
00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

输出:

67890
-1

五、完整C语言代码

AC代码~#include<stdio.h>
#include<stdlib.h>typedef struct Linklist {char ch;struct Linklist* next;int add;int nextadd;
} L;int Linklen(L* h) {  // 求链表长度L* tmp = h->next;int len = 0;while (tmp != NULL) {len++;tmp = tmp->next;}return len;
}int main() {int n, f1, f2;while (scanf("%d%d%d", &f1, &f2, &n) != EOF) {L* a = (L*)malloc(n * sizeof(L));  // 结构体数组L* h1 = (L*)malloc(sizeof(L)); // 头节点L* h2 = (L*)malloc(sizeof(L));for (int i = 0; i < n; i++)scanf("%d %c %d", &a[i].add, &a[i].ch, &a[i].nextadd);int head1, head2;for (int i = 0; i < n; i++) { // 找出第一个节点的编号if (a[i].add == f1)head1 = i;if (a[i].add == f2)head2 = i;}h1->next = NULL;h2->next = NULL;h1->next = &a[head1];h2->next = &a[head2];L* tail1 = h1->next;       // 尾指针L* tail2 = h2->next;while (tail1->nextadd != -1) { // h1链连接好int i = 0;for (i = 0; i < n; i++) {if (a[i].add == tail1->nextadd) {tail1->next = &a[i];tail1 = &a[i];break;}}}tail1->next = NULL;while (tail2->nextadd != -1) { // h2链连接好int i = 0;for (i = 0; i < n; i++) {if (a[i].add == tail2->nextadd) {tail2->next = &a[i];tail2 = &a[i];break;}}}tail2->next = NULL;int len1 = Linklen(h1);int len2 = Linklen(h2);int dist;L* longL;            // long指向较长的链表L* shortL;           // short指向较短的链表if (len1 >= len2) {  // dist为二者长度差值longL = h1->next;shortL = h2->next;dist =  len1 - len2;} else {longL = h2->next;shortL = h1->next;dist =  len2 - len1;}while (dist > 0) {  // 较长的先走dist步 目的是使二者对齐dist--;longL = longL->next;}longL = longL->next;  // 二者都走一步(第一个节点就相等不算“公共后缀”)shortL = shortL->next;while (longL != NULL) {   // 相等的节点则是公共后缀的第一个if (longL->ch == shortL->ch && longL->nextadd == shortL->nextadd) {printf("%d\n", longL->add);break;}longL = longL->next;shortL = shortL->next;}if (longL == NULL)printf("-1\n");}return 0;
}

这篇关于题记(26)--Sharing(链表公共后缀)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/638051

相关文章

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Python在固定文件夹批量创建固定后缀的文件(方法详解)

《Python在固定文件夹批量创建固定后缀的文件(方法详解)》文章讲述了如何使用Python批量创建后缀为.md的文件夹,生成100个,代码中需要修改的路径、前缀和后缀名,并提供了注意事项和代码示例,... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5.

Linux下MySQL8.0.26安装教程

《Linux下MySQL8.0.26安装教程》文章详细介绍了如何在Linux系统上安装和配置MySQL,包括下载、解压、安装依赖、启动服务、获取默认密码、设置密码、支持远程登录以及创建表,感兴趣的朋友... 目录1.找到官网下载位置1.访问mysql存档2.下载社区版3.百度网盘中2.linux安装配置1.

csu1329(双向链表)

题意:给n个盒子,编号为1到n,四个操作:1、将x盒子移到y的左边;2、将x盒子移到y的右边;3、交换x和y盒子的位置;4、将所有的盒子反过来放。 思路分析:用双向链表解决。每个操作的时间复杂度为O(1),用数组来模拟链表,下面的代码是参考刘老师的标程写的。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#

poj1330(LCA最近公共祖先)

题意:求最近公共祖先 思路:之前学习了树链剖分,然后我就用树链剖分的一小部分知识就可以解这个题目了,记录每个结点的fa和depth。然后查找时,每次将depth大的结点往上走直到x = y。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>

深入手撕链表

链表 分类概念单链表增尾插头插插入 删尾删头删删除 查完整实现带头不带头 双向链表初始化增尾插头插插入 删查完整代码 数组 分类 #mermaid-svg-qKD178fTiiaYeKjl {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-

建立升序链表

题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2744 解决:1186 题目描述: 建立一个升序链表并遍历输出。 输入: 输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数。 输出: 可能有多组测试数据,对于每组数据, 将n个整数建立升序链表,之后遍历链表并输出。 样例输

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

学习记录:js算法(二十八):删除排序链表中的重复元素、删除排序链表中的重复元素II

文章目录 删除排序链表中的重复元素我的思路解法一:循环解法二:递归 网上思路 删除排序链表中的重复元素 II我的思路网上思路 总结 删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 图一 图二 示例 1:(图一)输入:head = [1,1,2]输出:[1,2]示例 2:(图