题目

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 sufix. For example, “loading” and “being” are stored as showed 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 sufix. If the two words have no common sufix, 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

题目分析

两个链表,有共同后缀,找出共同后缀的第一个节点的地址

解题思路

思路01(最优)

  1. 定义Node结构体,记录每个节点信息(地址,data,next指针),用map记录映射关系<地址,节点>方便O(1)查找(可以使用大数组代替,下标及地址,元素值记录next)
  2. 遍历第一个链表,出现过的元素标记为1
  3. 遍历第二个链表,若其节点在第一个链表中出现过(即:标记=1),则打印,若链表2中没有在链表1中出现过的节点,打印-1

思路02

  1. 定义Node结构体,记录每个节点信息(地址,data,next指针),用map记录映射关系<地址,节点>方便O(1)查找(可以使用大数组代替,下标及地址,元素值记录next)
  2. 分别计算两个链表的长度
  3. 先遍历长链表到其长度跟短链表长度相等
  4. 同时遍历两个链表,找出相同节点,若有相同节点,打印其地址,若无相同节点,打印-1;

思路03(该思路错误,列出来为了下次不再这样思考)

1. 记录输入的每个样例的next,如果有next出现两次,即为相同节点(×);如果其中一个链表的起点是另一个链表的节点(即任意一个链表的起点在另外一个链表中出现过)(测试点2,4,5错误)

注:网上其他朋友有说 一个错误情况为格式,必须为5个字符

Code

Code 01

  1. #include <iostream>
  2. #include <list>
  3. #include <unordered_map>
  4. using namespace std;
  5. struct node {
  6. string adr;
  7. char data;
  8. string next;
  9. bool flag=false;
  10. };
  11. int main(int argc,char * argv[]) {
  12. string s1,s2;
  13. int d;
  14. char c;
  15. node n;
  16. cin>>s1>>s2>>d;
  17. unordered_map<string,node> m; //用map最后一个点会超时
  18. for(int i=0; i<d; i++) {
  19. cin>>n.adr>>n.data>>n.next;
  20. m[n.adr]=n;
  21. }
  22. string n1=s1;
  23. while(n1!="-1") {
  24. m[n1].flag=true;
  25. n1=m[n1].next;
  26. }
  27. string n2=s2;
  28. while(n2!="-1") {
  29. if(m[n2].flag) {
  30. break;
  31. }
  32. n2=m[n2].next;
  33. }
  34. printf("%s",n2.c_str());
  35. return 0;
  36. }

Code 02

  1. #include <iostream>
  2. #include <list>
  3. #include <unordered_map>
  4. using namespace std;
  5. struct node {
  6. string adr;
  7. char data;
  8. string next;
  9. bool flag=false;
  10. };
  11. int main(int argc,char * argv[]) {
  12. string s1,s2;
  13. int d;
  14. char c;
  15. node n;
  16. cin>>s1>>s2>>d;
  17. unordered_map<string,node> m; //用map最后一个点会超时
  18. for(int i=0; i<d; i++) {
  19. cin>>n.adr>>n.data>>n.next;
  20. m[n.adr]=n;
  21. }
  22. int len1=0,len2=0;
  23. string n1=s1;
  24. while(n1!="-1") {
  25. n1=m[n1].next;
  26. len1++;
  27. }
  28. string n2=s2;
  29. while(n2!="-1") {
  30. n2=m[n2].next;
  31. len2++;
  32. }
  33. n1=s1,n2=s2;
  34. while(len1>len2) {
  35. len1--;
  36. n1=m[n1].next;
  37. }
  38. while(len1<len2) {
  39. len2--;
  40. n2=m[n2].next;
  41. }
  42. while(n1!=n2){
  43. n1=m[n1].next;
  44. n2=m[n2].next;
  45. }
  46. printf("%s",n1.c_str());
  47. return 0;
  48. }

PAT Advanced 1032 Sharing(25) [链表]的更多相关文章

  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 (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  3. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  4. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  5. PAT 1032 Sharing[hash][链表][一般上]

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  6. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  7. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

  8. 1032 Sharing (25分)(数组链表)

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

  9. PAT 1032 Sharing (25分) 从自信到自闭

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

随机推荐

  1. Linux基础命令层级图-01

    Linux基础命令层级图-01:

  2. Http协议Get与Post请求

    摘要:https://blog.csdn.net/kebi007/article/details/103059900 不就是get拼接url,post传body,get限制字符串长度吗! 请求缓存:G ...

  3. 【剑指Offer】面试题11. 旋转数组的最小数字

    题目 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个 ...

  4. UVA - 12118 Inspector's Dilemma(检查员的难题)(欧拉回路)

    题意:有一个n个点的无向完全图,找一条最短路(起点终点任意),使得该道路经过E条指定的边. 分析: 1.因为要使走过的路最短,所以每个指定的边最好只走一遍,所以是欧拉道路. 2.若当前连通的道路不是欧 ...

  5. 15 ~ express ~ 用户数据分页原理和实现

    一,在后台路由 /router/admin.js 中 1,限制获取的数据条数 : User.find().limit(Number) 2,忽略数据的前(Number)条数据 : skip(Number ...

  6. 【LeetCode】最小路径和

    [问题]给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [,,], [,,], [, ...

  7. java课程之团队开发冲刺阶段2.6

    总结昨天进度: 1.总体的思路已经完成,代码也差不多了,只剩下对闹钟activity的设置 遇到的困难: 1.在设置震动的时候,对方法有点不太理解,所以使用的时候产生了错误,没有达到预期的效果 今天的 ...

  8. 2.10 Jetpack LiveData部分

    LiveData是一个可观察的数据持有者类,但和其他的可观察对象不同,它与生命周期相关联,比如Activity的生命周期.LiveData能确保仅在Activity处于活动状态下才会更新.也就是说当观 ...

  9. 数据库连接池C3P0的使用

    一.直接使用代码链接(一般企业开发不会用到.大多数用方法二) 1.导入jar 2.新建JDBCUtil import java.io.FileInputStream; import java.io.I ...

  10. VS常用高效快捷键

    快捷键的使用能够提供我们写代码的效率.还能装逼(哈哈O(∩_∩)O~) 类别 快捷键 描述 编辑 Ctrl+S 保存(养成好习惯,停下来的时候就保存下,不然遇见突发情况会很崩溃的) Ctrl+Shif ...