PAT甲级——A1032 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.

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的更多相关文章
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1032 Sharing
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
随机推荐
- com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
报错: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected estab ...
- LightOJ-1259-Goldbach`s Conjecture-素数打表+判断素数对数
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathemat ...
- PHP面向对象魔术方法之__clone函数
l 基本介绍 : 当我们需要将一个对象完全的赋值一份, 保证两个对象的属性和属性值一样,但是他们的数据库空间独立,则可以使用对象克隆. <?php header('content-type:te ...
- 6_1.springboot2.x整合JDBC与数据源配置原理解析
1.引言 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入各种xxxTemplate,x ...
- LeetCode 8.字符串转换整数 (atoi)(Python3)
题目: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该 ...
- R语言进行广州租房可视化
又到了一年一度的换租房的季节,在广州,想要找到一处好一点的租房真心不容易,不是采光不好,就是价格太贵,怎么才能找到合适自己的房子呢?于是我利用“造数”这个虫工具爬取了安居客网的广州租房的数据,通过分析 ...
- xcart-子分类/语言不显示
由于数据库的版本不同,表名会区分大小写的情况,这样就导致xcart语言相关的信息显示不出来,此时只需更改根目录下的init.php文件,如下: 改成小写的就OK了.
- data hazard in CPU pipeline
1, background info 5 stages in CPU pipeline: IF, ID, EX, MM, WB IF – Instruction Fetch ID – Instruct ...
- thinkphp 快捷查询
快捷查询方式是一种多字段查询的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询,例如: 大理石平台价格 一.不同字段相同的 ...
- 安装zabbix需要php的两个模块php-bcmath和php-mbstring(转)
安装zabbix需要php的两个模块php-bcmath和php-mbstring 原创 Linux操作系统 作者:甲骨文技术支持 时间:2018-02-24 18:35:24 1472 0 1. ...