题目地址:点击打开链接

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 (<= 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 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

题意:输入两个单词的各位字符和地址和下一位字符地址,输出公共的字符部分的第一个字符的地址。

解法:把第一个单词的每一个字符放进集合set ,把第二个单词里的字符在set里进行查找,找到了就输出这个字符的地址。

我的C++代码:

#include <iostream>
#include<set>
using namespace std;
int main()
{int n, pointer1, pointer2 ;//n结点总个数, pointer1第一个单词首结点地址int nodeaddress[100000]; //数组保存各结点地址set<int>word1charset; //单词1字符组成的集合int addr, next; //addr地址,next下一个结点地址char ch; //结点字符信息cin >> pointer1 >> pointer2 >> n;for (int i = 0; i<n; i++){cin >> addr >> ch >> next;nodeaddress[addr] = next;//保存下一个结点的地址}while (pointer1 != -1)//当单词1字符没被遍历完{word1charset.insert(pointer1);//把单词1的字符添加进setpointer1 = nodeaddress[pointer1];//地址向后移}while (pointer2 != -1)//当单词2字符没被遍历完{if (word1charset.count(pointer2) != 0)//找到了相同的元素{printf("%05d", pointer2);//输出相同的字符的地址return 0;  //结束退出程序}pointer2 = nodeaddress[pointer2];//地址向后移}printf("-1");return 0;
}

浙江大学PAT_甲级_1032. Sharing (25)相关推荐

  1. PAT甲级 1032 Sharing (25分) 测试点5陷阱

    题目 1032 Sharing 分析 suffix是后缀,题目的意思是求两个单词的公共后缀的第一个字符的地址.我看有些博客说求的是首个共用结点的地址,我觉得是不对的. 晴神/柳神的解法,是把第一个单词 ...

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

    文章目录 题目分析 题目链接 题目分析 思路:建立链表,这里使用数组.e [ ] 表示当前的数值,ne[ ]表示next指针. 求单词的公共后缀,就是求两个链表从头结点开始第一个公共的结点. 先遍历第 ...

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

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

  4. 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...

  5. 【测试点2分析】:1020 月饼 (25分)(甲级 1070 Mooncake (25 分))

    立志用更少的代码做更高效的表达 PAT甲级最优题解-->传送门 Pat乙级最优化代码+题解+分析汇总-->传送门 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现 ...

  6. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  7. PAT_甲级 简单模拟

    A1006 Sign In and Sign Out (25) 简单模拟 原题 At the beginning of every day, the first person who signs in ...

  8. *PAT_甲级_1072 Gas Station (30point(s)) (C++)【Dijkstra/字符串截取/与数字相互转换】

    目录 1,题目描述 题目大意 注意: 2,思路 数据结构 算法 3,AC代码 4,解题过程 第一搏 第二搏 1,题目描述 Sample Input 1: 4 3 11 5 1 2 2 1 4 2 1 ...

  9. **PAT_甲级_1057 Stack (30分) (C++)【字符串处理/栈的模拟/树状数组】

    目录 1,题目描述 题目大意 2,思路 数据结构 函数讲解 1,void update(int x, int v): 2,int getsum(int x): 3,void PeekMedian(): ...

最新文章

  1. IDEA一直提示 错误: 找不到或无法加载主类
  2. python之集合操作
  3. unityAPI文档离线下载
  4. 大数据学习入门必备规划
  5. 自动化技术、计算机技术核心期刊整理及介绍
  6. 微软的产品激活中心电话
  7. Python Numpy random.poisson() 泊松分布
  8. 《计算机网络--自顶向下方法》第三章--运输层
  9. 保时捷上海车展阵容公布:新款Cayenne全球首秀
  10. 怎么把raw转换成jpg格式?推荐两个raw转jpg的方法
  11. 计算机从bios到操作系统,高手进!!计算机开机后在进入操作系统前BIOS在进行什么检测工作?...
  12. Qt 虚拟键盘实现中文输入
  13. 查看电脑上安装的所有的python版本以及安装路径
  14. 钱多多第二阶段冲刺05
  15. matlab怎么提取小数部分,[转载]如何在Matlab中得到一个单、双精度数的整数部分和小数部分?...
  16. 轮播图 --- 无缝连接的轮播图
  17. 国家/行业标准查询及下载全流程
  18. 公务员年度考核登记表
  19. RHCE认证资料2021.8.28
  20. VM虚拟机安装凝思磐石系统(字符界面)

热门文章

  1. 微信小程序 如何返回上一个页面并实现刷新
  2. MOSFET的误启动发生机制-3
  3. SQL中的查询语句总结(实例)
  4. 邦纳传感器M18TUP8Q
  5. IM即时通信分布式后端方案(分布式websocket)
  6. 基于51单片机的压力监测仪(MPX4115)(Proteus仿真+程序)
  7. java 根据PDF模板生成PDF文件并导出
  8. mdadm彻底删除software RAID
  9. 笔记整理--Linux守护进程
  10. 汇编程序的汇编及运行