题目描写叙述

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).

输入描写叙述:

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.

输出描写叙述:

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.

输入样例:

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

输出样例:

67890
//最初的解法低效。且有未知错误
/*思路例如以下:
从最后面開始找。直到存在某个前驱有不止一个后继指向它,则结束*/
#include <iostream>
#include <iomanip>using namespace std;typedef struct Node
{int addressPre,addressPost;char data;
};int main()
{int a1,a2,N,i,j,k,ans;while(cin>>a1>>a2>>N){Node *node=new Node[N];for(i=0;i<N;i++)cin>>node[i].addressPre>>node[i].data>>node[i].addressPost;
/*for(i=0;i<N;i++)cout<<node[i].addressPre<<" "<<node[i].data<<" "<<node[i].addressPost<<endl;
*/k=-1;while(1){ans=0;for(i=0;i<N;i++){if(k==node[i].addressPost){ans++;j=i;}      }if(ans>1)break;elsek=node[j].addressPre;}cout <<setfill('0')<< setw(5)<<k<<endl;}return 0;
}

正解

#include <iostream>
#include <iomanip>
#include <vector>using namespace std;typedef struct Node
{int addressPre,addressPost;char data;
};vector<Node> list1;
vector<Node> list2;Node node[100010];int main()
{int a1,a2,N,i,j,k;int l1,l2;Node tNode;while(cin>>a1>>a2>>N){for(i=0;i<N;i++){cin>>tNode.addressPre>>tNode.data>>tNode.addressPost;node[tNode.addressPre]=tNode;}l1=a1;l2=a2;while(l1!=-1){list1.push_back(node[l1]);l1=node[l1].addressPost;}while(l2!=-1){list2.push_back(node[l2]);l2=node[l2].addressPost;}for(i=0,k=0;i<list1.size();i++){for(j=0;j<list2.size();j++){if(list1[i].addressPre==list2[j].addressPre){k=1;break;}}if(k)break;}if(k)cout <<setfill('0')<< setw(5)<<list1[i].addressPre<<endl;elsecout<<-1<<endl;}return 0;
}

转载于:https://www.cnblogs.com/clnchanpin/p/7202242.html

PAT 1003 Sharing (25)相关推荐

  1. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

  2. 7-227 PAT排名汇总 (25 分)

    7-227 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设 ...

  3. PAT 1003 Emergency(最短路(迪杰斯特拉||贝尔曼)最小边权下的最大点权)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. 1085 PAT单位排行 (25分)-PAT乙级真题-C++实现

    1085 PAT单位排行 (25分)-PAT乙级真题-C++实现 题目描述: 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整 ...

  5. PTA 天梯赛 7-41 PAT排名汇总 (25 point(s))

    7-41 PAT排名汇总 (25 point(s)) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法 ...

  6. 1095 解码PAT准考证 (25分)击破测试点3、4,50ms内通关

    1095 解码PAT准考证 (25分)测试点34用时低于35ms 前言 一.题目简介 二.原题内容 1. 设定 2. 输入格式 3. 输出格式 三.题目分析 1. 要求1分析 2. 要求2分析 3. ...

  7. 1003 Emergency (25 分)

    1003 Emergency (25 分) 题目大意 n个城市m条路,每个城市有救援小组,所有的边的边权已知.给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和.如果有多条就 ...

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

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

  9. PAT:1032. Sharing (25) AC

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

最新文章

  1. 深度丨AI界的七大未解之谜:OpenAI丢出一组AI研究课题
  2. Spark详解(十一):Spark运行架构原理分析
  3. 创建一个提供数据 API 的 Node.js 网站
  4. nexus 6p Android SDK,Flutter没有检测到Android SDK
  5. 程序员面试金典 - 面试题 01.06. 字符串压缩(字符串)
  6. 世界首富比尔·盖茨的母亲有多厉害?
  7. python日期对照表_2020年日期表-python实现
  8. Cisco交换机上的链路聚合
  9. 5g理论速度_快看看 5G 的实际网速,失望还是兴奋?
  10. stm32单片机OLED显示图片 位图转换 Image2Lcd使用
  11. python的ols_工具方法 | 6行代码教你用Python做OLS回归(内附CFPS实例)
  12. 「Linux」- 修改鼠标滚轮的滚动方向 @20210315
  13. OI游记——一个不配称为OIer的失败选手的自白
  14. Notepad ++中的一个著名插件FingerText
  15. 英文学术论文写作——模式识别方向(笔记)
  16. select2回显操作
  17. uniapp获取微信授权登录和手机号一键登录(保姆教程)
  18. 为什么iPhone通常比Android具有更好的音质?
  19. 听诊器的基本构造及其特征
  20. Angular 实现树形菜单(多级菜单)功能模块

热门文章

  1. CPU核数和线程数查找
  2. springboot系列四、配置模板引擎、配置热部署
  3. 01_13_JSP编译指令
  4. 看周志华教授的一番话有感
  5. VS2015 中使用 MVC4
  6. JavaEE班第四天
  7. 吐血整理!12种通用知识图谱项目简介
  8. 那两个告扎克伯格抄袭的斜杠青年,后来怎么样了?
  9. 数据分析与数据化运营的关键知识点,全在这里了
  10. 怎样写出别人无法维护的代码