【PAT甲级 - C++题解】1032 Sharing

每天进步一点点:PAT 甲级 1032! PAT 甲级 1032 阅读详情

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1032 Sharing (pintia.cn)
🔑中文翻译:共享
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6A9PvymA-1672189065513)(PAT 甲级辅导.assets/ef0a1fdf-3d9f-46dc-9a27-21f989270fd4.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 (≤105), 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
题意

给定两个链表的结点信息,每个链表中存储的是一个单词,需要找出两个单词中共同后缀的起始地址,即找到两个链表的第一个共同结点的地址,如果没有共同后缀则输出 -1 。

第一行输入的是两个链表的起始节点的地址以及结点的总数量 n ,下面 n 行输入每个结点的地址、数据和下一个结点的地址,其中 -1 表示 NULL 。

思路

具体思路如下:

  1. 先用数组将链表中结点的地址和数据存储起来。
  2. 用一个 bool 数组 st 来标记链表 1 中所有结点为 true 。
  3. 从头遍历链表 2 中的结点,只要出现第一个在 st 中标记为 true 的结点,则直接输出。
  4. 如果没有找到共同结点,则输出 -1 。
代码
#include<bits/stdc++.h>
using namespace std;

const int N = 100010;
int h1, h2, n, ne[N];
char e[N];
bool st[N];

int main()
{
    //输入每个结点的信息
    scanf("%d%d%d", &h1, &h2, &n);
    for (int i = 0; i < n; i++)
    {
        int address, next;
        char c;
        scanf("%d %c %d", &address, &c, &next);
        e[address] = c, ne[address] = next;
    }

    //标记链表1中的结点
    for (int i = h1; i != -1; i = ne[i])
        st[i] = true;

    //输出链表2中第一个与链表1共同的结点
    for (int i = h2; i != -1; i = ne[i])
        if (st[i])
        {
            printf("%05d\n", i);
            return 0;
        }
    puts("-1"); //不存在则输出-1

    return 0;
}
PAT甲级 1032 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, and are stored as showed in Figure 1.Figure 1You are 阅读详情

相关推荐

Debian 12 安装nodejs(包括node、npm、npx、yarn、pnpm、cnpm、bun)

【代码】Debian 12 安装nodejs。

mirana的专栏 8082

信息学奥赛一本通(c++)题号:1032

1032:大象喝水查 题目描述 一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。 输入 输入有一行:包行两个整数,以一个空格分开,分别表示小圆桶的深h和底面半径r,单位都是厘米。 输出 输出一行,包含一个整数,表示大象至少要喝水的桶数。 输入样例 23 11 输出样例 3 代码 #include<iostream> #include<cmath> using namespac

北港 1723

锂电池主动均衡理解

锂电池主动均衡方案总结: 来自:http://wenku.baidu.com/link?url=EWiyACZ3aLPhcOjI4FZL3uQ9d0FD6dBhADQSVQiaSmRHHATTdSig1Wy39JY7-ijCzbAEYG1ymz6Lv5zhGdZaStQSHzFt6v3h3_tMrJudz9O 目前车厂采用被动均衡方案的公司:(来自网页) 目前采用主动均衡的...

weixin_34365417的博客 2916

PAT甲级1032 Sharing :[C++题解]链表

刷题

阿正的梦工坊 528

深入解析SPLL单相软件锁相环:从SOGI原理到PI系数调优实践

本文深入解析SPLL单相软件锁相环技术,从SOGI原理到PI系数调优实践,详细介绍了其在数字电源设计中的应用。通过数学推导和实际调试经验,帮助工程师快速掌握锁相环的核心参数计算和优化方法,提升电网相位追踪的精确度和响应速度。

weixin_30316097的博客 390

1032 Sharing——PAT甲级(测试点4分析)

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, and are stored as showed in Figure 1.Figure 1You are

flyidj的博客 1946

【Educoder作业】※ 数字图像——绿幕抠像

这次作业比上一篇博客基本得多,大多都是基础函数的运用。没啥说的,就整个sizesizesize即可。 T2 获取一个像素 T3 图像反色 T4 修改透明度 ′RGBA′'RGBA'′RGBA′也是一种图像格式 T5 抠图 T6 添加场景 这个主要是pastepastepaste函数里面的maskmaskmask参量没整明白,转载一篇博客供参考。...

JZYshuraK的博客 7581

PAT甲级1032、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, and are stored as showed in Figure 1.Figure 1You are

geooorge的博客 575

Flink实战案例(十三):Flink DataStream 八大分区策略与自定义分区器

声明:本系列博客是根据SGG的视频整理而成,非常适合大家入门学习。 《2021年最新版大数据面试题全面开启更新》 分区策略决定了一条数据如何发送给下游。Flink中默认提供了八大分区策略(也叫分区器)。 本文基于Flink 1.9.0总结Flink DataStream中的八大分区策略以及手动实现一个自定义分区器。 八大分区策略继承关系图 ChannelSelector: 接口,决定将记录写入哪个Channel。有3个方法: void setup(int numberOfChannels):

微信搜:import_bigdata,大数据领域硬核原创作者 1130

【Android 教程系列第 30 篇】为什么要为 App 应用加固 ?如何为 App 应用加固 ?

一:为什么要为 App 应用加固 ? 二:如何为 App 应用加固 ? 三:加固后为什么要重新签名 ?

“Allen Su”的博客 7059

题143.pat甲级练习-1032 Sharing (25 分)

文章目录题143.pat甲级练习-1032 Sharing (25 分)一、题目二、题解 题143.pat甲级练习-1032 Sharing (25 分) 一、题目 二、题解 题目要求找到共用链表的表头的地址输出,我采用的路子是分开遍历两个单词,把遍历到的节点地址对应flag++,然后看谁最先flag=2就输出。 #include <bits/stdc++.h> using namespace std; char data0[100000]; int flag[100000],n

Adongua的博客 170

通过Python实现物联网金橙子自动打标

对于自家公司使用金橙子打标卡时,为了与生产线或者PLC联动等,往往需要对金橙子打标卡进行二次开发,而在二次开发中不得不涉及到MarkEzd.dll这个文件,而这个dll文件网上版本多不说,一般都没法直接使用,最后还不得不花大价钱购买专用库。而本文针对这个问题,涉法通过python来绕过MarkEzd.dll文件,直接进行自动化控制EzCad来实现联动,可应用于实际生产。文章最后拓展一下以物联网的形式,实现手机平台等登录网页,输入想要的印字内容后,远程打印的案例。

lylelo的博客 3372

PAT 甲级 1032 Sharing

PAT 甲级 1032 Sharing #include <bits/stdc++.h> using namespace std; const int kMAXN = 100000; struct Node { int address, next, flag{false}; char data; } node[kMAXN]; int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif i

桂圆儿的仓库 113

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PAT 甲 1032 Sharing

2022.1.22 练习 PAT甲 1032 Sharing(原题链接) 题解如下: #include<bits/stdc++.h> using namespace std; const int MAX_SIZE=1e6+5; struct Node { string data; int next; int flag; }node[MAX_SIZE]; int main() { std::ios::sync_with_stdio(false); int

想买很多漂亮衣服但是买不起的博客 232

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

PAT甲级-1032 Sharing

链表的遍历 C++

weixin_74092648的博客 342

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PAT甲级1032 Sharing

AC代码 #include<bits/stdc++.h> using namespace std; struct Node{ int next; char data; }node[100001]; int main() { int a1,b1,n,a,next; char c; scanf("%d%d%d",&a1,&b1,&am...

m0_46515304的博客 206

【PAT甲级题解】1032 Sharing (25分)

本题考链表处理,题意大致是有两个英文单词组成的链表,但是为了节省空间,对于存在相同后缀’suffix’的单词,链表选择使用相同的指针,例如loding和being,后缀都是ing所以,对于loding,d字母的指针就指向了i,对于being,e字母的指针就指向了i,从此之后,他们共用余下的指针,本题就是要让你找出那第一个指针的位置,并输出它的地址,这道题有两种方法,一种最容易的就是暴力模拟,从任意...

qq_45566099的博客 253

PAT甲级1031~1035|C++实现

PAT甲级1031 Hello World for U (20分)|C++实现 PAT甲级1032 Sharing (25分)|C++实现 PAT甲级1033 To Fill or Not to Fill (25分)|C++实现 PAT甲级1034 Head of a Gang (30分)|C++实现 PAT甲级1035 Password (20分)|C++实现

weixin_42393947的博客 181

PAT甲级1032 Sharing (25 分)

1032Sharing(25分) 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...

Dexter 207

PAT甲级-1032-Sharing(链表的遍历)

链表的遍历和图的遍历一样,需要设计一个标记变量,标记每个结点node是否被访问过。 什么是相同的结点?其实地址相同的结点就是相同结点。什么是公共结点?即遍历完两条单词链,访问过两次的结点。 ...

波点兔的博客 1688

1032. Sharing (25)-PAT甲级真题

1032. Sharing (25) 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 s

柳婼 の blog 2138

【PAT甲级、C++】1032 Sharing (25分)

我知道这么写暴力法肯定会有问题,但还是尝试了下,果然最后两个测试点不通过 # include <iostream> using namespace std; int AddrNow[100010]; char Info[100010]; int AddrNxt[100010]; int N; int main() { int start1, start2; cin >> start1 >> start2 >> N; for(int

MYMarcoreus的博客 243

浙大 PAT 甲级 1032 Sharing C++ 部分错误原因

思路 检测链表相交问题,如果能够想清楚题目核心的话就非常简单,不需要用任何指针、结构体,输入中给出的字符也完全用不上。 定义一个数组int word[100000],五位地址,因此数组大小为10的6次方即可,全部初始化为-1,代表NULL。下标代表当前单元地址,数组的实际内容代表指向的下一个单元地址。这样就简简单单地实现了链表。 求两个链表是否相交,也不需要进行时间复杂度的暴力求解,这样会超...

qq_39115541的博客 423
上一篇: 【PAT甲级 - C++题解】1033 To Fill or Not to Fill
下一篇: 【PAT甲级 - C++题解】1074 Reversing Linked List
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值