题目

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 sufix. For example, “loading” and “being” are stored as showed 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

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.

Output Specification:

For each case, simply output the 5-digit starting position of the common sufix. If the two words have no common sufix, 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

题目分析

两个链表,有共同后缀,找出共同后缀的第一个节点的地址

解题思路

思路01(最优)

  1. 定义Node结构体,记录每个节点信息(地址,data,next指针),用map记录映射关系<地址,节点>方便O(1)查找(可以使用大数组代替,下标及地址,元素值记录next)
  2. 遍历第一个链表,出现过的元素标记为1
  3. 遍历第二个链表,若其节点在第一个链表中出现过(即:标记=1),则打印,若链表2中没有在链表1中出现过的节点,打印-1

思路02

  1. 定义Node结构体,记录每个节点信息(地址,data,next指针),用map记录映射关系<地址,节点>方便O(1)查找(可以使用大数组代替,下标及地址,元素值记录next)
  2. 分别计算两个链表的长度
  3. 先遍历长链表到其长度跟短链表长度相等
  4. 同时遍历两个链表,找出相同节点,若有相同节点,打印其地址,若无相同节点,打印-1;

思路03(该思路错误,列出来为了下次不再这样思考)

1. 记录输入的每个样例的next,如果有next出现两次,即为相同节点(×);如果其中一个链表的起点是另一个链表的节点(即任意一个链表的起点在另外一个链表中出现过)(测试点2,4,5错误)

注:网上其他朋友有说 一个错误情况为格式,必须为5个字符

Code

Code 01

#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
struct node {
string adr;
char data;
string next;
bool flag=false;
};
int main(int argc,char * argv[]) {
string s1,s2;
int d;
char c;
node n;
cin>>s1>>s2>>d;
unordered_map<string,node> m; //用map最后一个点会超时
for(int i=0; i<d; i++) {
cin>>n.adr>>n.data>>n.next;
m[n.adr]=n;
} string n1=s1;
while(n1!="-1") {
m[n1].flag=true;
n1=m[n1].next;
} string n2=s2;
while(n2!="-1") {
if(m[n2].flag) {
break;
}
n2=m[n2].next;
}
printf("%s",n2.c_str());
return 0;
}

Code 02

#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
struct node {
string adr;
char data;
string next;
bool flag=false;
};
int main(int argc,char * argv[]) {
string s1,s2;
int d;
char c;
node n;
cin>>s1>>s2>>d;
unordered_map<string,node> m; //用map最后一个点会超时
for(int i=0; i<d; i++) {
cin>>n.adr>>n.data>>n.next;
m[n.adr]=n;
} int len1=0,len2=0;
string n1=s1;
while(n1!="-1") {
n1=m[n1].next;
len1++;
}
string n2=s2;
while(n2!="-1") {
n2=m[n2].next;
len2++;
} n1=s1,n2=s2;
while(len1>len2) {
len1--;
n1=m[n1].next;
}
while(len1<len2) {
len2--;
n2=m[n2].next;
}
while(n1!=n2){
n1=m[n1].next;
n2=m[n2].next;
}
printf("%s",n1.c_str());
return 0;
}

PAT Advanced 1032 Sharing(25) [链表]的更多相关文章

  1. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  2. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  3. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  4. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  5. PAT 1032 Sharing[hash][链表][一般上]

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  6. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  7. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

  8. 1032 Sharing (25分)(数组链表)

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  9. PAT 1032 Sharing (25分) 从自信到自闭

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

随机推荐

  1. web前端知识点

    一.CSS问题 1.flex布局 display:flex; 在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩 flex:1; 子元素设置,设置子元素如何分配父元素的空间,fl ...

  2. 实验吧-密码学-try them all(加salt的密码)、robomunication(摩斯电码)、The Flash-14(闪电侠14集)

    try them all(加salt的密码) 首先,要了解什么事加salt的密码. 加salt是一种密码安全保护措施,就是你输入密码,系统随机生成一个salt值,然后对密码+salt进行哈希散列得到加 ...

  3. 修改ssh主机名

    三台机器的配置相似,以zk1为例 1.修改hostname vi /etc/hostname zk1 2.修改hosts文件 vi /etc/hosts 10.45.48.233 zk1 10.45. ...

  4. Elasticsearch 使用集群 - 创建索引

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  5. vue小练习--音乐播放器

    1 首先建一个文件夹 放几首歌曲 2 看代码 1)基本版本 <!DOCTYPE html> <html lang="zh-CN"> <head> ...

  6. Linux-课后练习(第二章命令)20200217-2

  7. jmeter --- 压测时,如何让串联压测多个接口

    1.新建测试计划,在测试计划里勾选“独立运行每个线程组” 2.分别在每个线程组,添加聚合报告,以得到每个线程组的压测结果 3.设置不同接口压测的vu等,并为每个线程组,设置合理的启动延迟时间

  8. 【Vue中的坑】路由相同参数不同无法触发路由

    场景: vue实现导航栏,二级导航栏跳转到相同页面,通过参数来实现到该页面后,根据参数来滚动到对应到位置 网上的解决方法: 通常情况下我们喜欢设置keepAlive 包裹 router-view &l ...

  9. java类加载及new对象的过程

    /* SubClass sub = new SubClass(); 这句话到底做了什么事情呢? 1.javac编译.java源文件形成.class字节码文件; 2.new SubClass()对象时, ...

  10. MVC学生管理系统-阶段II(添加学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示    请看上一篇文章 本文是对阶段 ...