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.

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 (≤1e5), 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

这题感觉网上都写得好复杂,我心想第一个公共后缀的地址,不就是在输入的时候出现两次就可以了嘛,然后就用 mapmapmap 记录了一下,交了一发 222222 分,最后一个点错了,想了想很快就发现可能两个单词在某一个字符的地址是一样的,随后又分支出去了,所以这也很好改,只要再标记一下这个地址作为前缀出现了几次就行,也即第一个公共后缀地址的条件就是:作为前缀只出现了一次,作为后缀出现了两次,用两个 mapmapmap 标记即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){map<int,int>m1,m2;int a,b,x,y,n,ans=-1;char c;scanf("%d%d%d",&a,&b,&n);while(n--){scanf("%d %c %d",&x,&c,&y);m1[x]++;m2[y]++;}if(a==b) ans=a;for(auto i:m1){if(i.second==1&&m2[i.first]==2) ans=i.first;}if(ans>0) printf("%05d",ans);else printf("-1");
}

PAT甲级 1032 Sharing相关推荐

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

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

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

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

  3. PAT:1032. Sharing (25) AC

    #include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char data; i ...

  4. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

  5. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  6. PAT甲级训练合集(1-70)

    本章题解跳转 考点 P1001 数字的数组表示和处理 P1002 多项式的数组表示和处理 P1003 深度优先搜素 P1004 深度优先搜素 P1005 哈希表 P1006 P1007 数组子区间求和 ...

  7. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  8. PAT甲级题目对应知识点分类梳理

    PAT甲级的106道题的知识点与对应的题号整理如下,便于做专项练习和巩固! 1.数据结构 可以用STL系列 栈:1051 堆:1098 队列:1014.1056 链表:1032.1052.1074.1 ...

  9. PAT甲级考试题库题目分类

    PAT甲级题目分类 水题 1136.1139.1143.1148 字符串处理 1001.1005.1035.1061.1073.1077.1082. 1108.1140.1152 模拟 1002.10 ...

  10. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

最新文章

  1. 内部类--毕向东Java基础教程学习笔记
  2. html文字自动上翻,jQuery超酷文字随机翻转变换动画特效
  3. 洛谷 4364 [九省联考2018]IIIDX——“预留”的思路
  4. 汇编语言——《分支与循环程序设计》实验报告
  5. mysql数据的引用_MySQL数据库入门:mysql操作和程序引用
  6. javascript --- [jsonp] script标签的妙用(绕过同源限制)
  7. 练字格子纸模板pdf_高考英语作文模板(总结八种常考题型,配合例文,纯手打的)...
  8. 【Lucy-Richardson去卷积】迭代加速算法
  9. Upload-Labs(16)
  10. GitHub|基于强化学习自动化剪枝
  11. CentOs 设置静态IP 方法[测试没问题]
  12. 身份证号码(最后一位)计算。(使用 Excel 公式,计算 身份证最后一位)
  13. Linux目录结构示意详解图
  14. snowflake算法
  15. 全网首发:怎样制作CDKEY(7)-新思路
  16. 设计模式 ( 二十) 备忘录模式
  17. 用户运营day3——用户生命周期与用户成长路径的策略运营
  18. python自动添加微信好友_Python实现微信自动好友验证,自动回复,发送群聊链接方法...
  19. Java简易图书DVD管理系统
  20. 老挑毛 win7 linux,老挑毛u盘启动工具下载

热门文章

  1. 浅谈在windows系统下esp8266和esp32开发共存一个eclipse编译器,非安信可一体化环境,而是搭建自己的eclipes环境。
  2. 计算机管理中其它设备问号,Win10设备管理器中设备上有白色问号如何解决
  3. FineReport报表设计基础
  4. 软件测试是做什么的?好学的吗?
  5. 特斯拉硬核皮卡发布,现场却「碎了一地玻璃」
  6. FPGA零基础学习:FPGA芯片简介
  7. wince 百度地图懒人包_百度地图winCE版 V10.9.2 安卓版
  8. DSP28335的eCAP模块
  9. 计算机绘图读后笔记3000字,0921计算机绘图0.doc
  10. 捋一捋Unified Language Model Pre-training for Natural Language Understanding and Generation