题记(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

相关文章

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Linux搭建单机MySQL8.0.26版本的操作方法

《Linux搭建单机MySQL8.0.26版本的操作方法》:本文主要介绍Linux搭建单机MySQL8.0.26版本的操作方法,本文通过图文并茂的形式给大家讲解的非常详细,感兴趣的朋友一起看看吧... 目录概述环境信息数据库服务安装步骤下载前置依赖服务下载方式一:进入官网下载,并上传到宿主机中,适合离线环境

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

使用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>