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.

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 (≤), 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 −.

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

本以为,节点next出现两次,该节点就是公共节点,也包括-1在内,在牛客上测试通过,但在pat上测试失败,后来才感觉pat上可能出现公共节点在头结点上,故使用flag标记法,两条链表走过相同节点,则为公共节点
 #include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
int head1, head2, N, addr, next;
char c;
cin >> head1 >> head2 >> N;
unordered_map<int, pair<int, bool>>list;
for (int i = ; i < N; ++i)
{
cin >> addr >> c >> next;
list[addr] = make_pair(next,false);
}
for (int i = head1; i != -; i = list[i].first)
list[i].second = true;
for (int i = head2; i != -; i = list[i].first)
{
if (list[i].second == true)
{
printf("%05d\n", i);
return ;
}
}
printf("-1\n");
return ;
}
 

PAT甲级——A1032 Sharing的更多相关文章

  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

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  6. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  7. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  8. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  9. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

随机推荐

  1. 国内外自然语言处理(NLP)研究组

     国内外自然语言处理(NLP)研究组 *博客地址 http://blog.csdn.net/wangxinginnlp/article/details/44890553 *排名不分先后.收集不全,欢迎 ...

  2. iis网站域名绑定

    这里解释两种绑定 一.全局ip未分配 二.绑定指定域名 1.添加主机名 注释:没有绑定主机名的进站默认就会被访问.绑定主机名的进站访问特定主机名的网站.

  3. K最近邻(KNN,k-Nearest Neighbor)准确理解

    K最近邻(KNN,k-Nearest Neighbor)准确理解 用了之后,发现我用的都是1NN,所以查阅了一下相关文献,才对KNN理解正确了,真是丢人了. 下图中,绿色圆要被决定赋予哪个类,是红色三 ...

  4. Linux 后台运行python .sh等程序,以及查看和关闭后台运行程序操作

    1.运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令. (1)&命令 功能:加在一个命令的最后,可以把这 ...

  5. Mysql优化系列之数据类型优化

    本篇是优化系列的第一篇:数据类型 为了不产生赘述,尽量用简洁的语言来描述. 在选择数据类型之前,首先要知道几个原则: 更小的通常更好 尽量使用可以正确存储数据的最小数据类型.更小的数据类型意味着更快, ...

  6. 微信小程序(mpvue框架) 购物车

    效果图: 说明:全选/全不选, 1.数据: products:[{checked:true,code:"4",echecked:false,hasPromotions:true,i ...

  7. demjson处理json数据

    因为json数据不规范出现了以下问题: json.decoder.JSONDecodeError: Expecting property name enclosed in double quo 网上查 ...

  8. 16.ajax_case05

    # 抓取36氪快讯 # https://36kr.com/newsflashes import requests import json header = { 'Accept': 'text/html ...

  9. bind()的模拟实现

    上一篇对call和apply的模拟实现做了一个梳理,可参见:模拟实现call.apply,下面将具体研究一下bind啦啦啦 1. bind和call/apply的差别 bind方法会创建一个新函数,返 ...

  10. php访问其他网站接口

    使用函数:  file_get_contents($url); 传入接口url及其参数:如 $url="http://192.168.1.1/test.jsp?id=1&type=2 ...