https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920

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:

  1. 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:

  1. 11111 22222 9
  2. 67890 i 00002
  3. 00010 a 12345
  4. 00003 g -1
  5. 12345 D 67890
  6. 00002 n 00003
  7. 22222 B 23456
  8. 11111 L 00001
  9. 23456 e 67890
  10. 00001 o 00010

Sample Output 1:

  1. 67890

Sample Input 2:

  1. 00001 00002 4
  2. 00001 a 10001
  3. 10001 s -1
  4. 00002 a 10002
  5. 10002 t -1

Sample Output 2:

  1. -1

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 1e5 + 10;
  5. int st1, st2, N;
  6.  
  7. struct Node{
  8. int address;
  9. char data[3];
  10. int next;
  11. };
  12.  
  13. Node n;
  14. vector<Node> l1, l2;
  15. Node ad1[maxn];
  16. map<int, int> mp;
  17.  
  18. int main() {
  19. scanf("%d%d%d", &st1, &st2, &N);
  20. for(int i = 1; i <= N; i ++) {
  21. scanf("%d%s%d", &n.address, n.data, &n.next);
  22. ad1[n.address] = n;
  23. }
  24.  
  25. int temp = -1;
  26. int fir1 = st1;
  27. while(fir1 != -1) {
  28. mp[fir1] ++;
  29. l1.push_back(ad1[fir1]);
  30. fir1 = ad1[fir1].next;
  31. }
  32. int fir2 = st2;
  33. while(fir2 != -1) {
  34. if(mp[fir2]) {temp = fir2; break;}
  35. l2.push_back(ad1[fir2]);
  36. fir2 = ad1[fir2].next;
  37. }
  38.  
  39. if(temp != -1)
  40. printf("%05d\n", temp);
  41.  
  42. else
  43. printf("-1\n");
  44. return 0;
  45. }

 

PAT 甲级 1032 Sharing的更多相关文章

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

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

  5. PAT Advanced 1032 Sharing(25) [链表]

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

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

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

  7. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. Linux 中的权限

    ABCD A-0, 十进制 B-user(u, 用户) C-group(g, 组用户) D-others(o, 其他用户) +-----+---+--------------------------+ ...

  2. java的编码格式

    几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...

  3. 利用Python Counter快速计算出现次数topN的元素

    需要用Python写一段代码,给定一堆关键词,返回出现次数最多的n个关键字. 第一反应是采用一个dict,key存储关键词,value存储出现次数,如此一次遍历即可得出所有不同关键词的出现次数,而后排 ...

  4. P1126 机器人搬重物

    P1126 机器人搬重物 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有 ...

  5. SpringBoot学习:整合MyBatis,使用Druid连接池

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)添加pom依赖: <!-- https://mvnrepository.co ...

  6. TraceHelper

    public class TraceHelper { private static TraceHelper _traceHelper; private TraceHelper() { } public ...

  7. textview的阴影线

    android:shadowColor="#000000" android:shadowDx="1" android:shadowDy="1" ...

  8. Python 函数参数类型大全(非常全!!!)

    Python 函数参数类型大全(非常全!!!) 1.在python编写程序里面具有函数文档,它的主要作用是为了让别人可以更好的理解你的函数,所以这是一个好习惯,访问函数文档的方式是: MyFuncti ...

  9. Python字符串操作大全(非常全!!!)

    1. python编程里字符串的内置方法(非常全) capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居 ...

  10. 关于Python 中的 if 语句

    学习Python,最开始我们都是先从函数学起,Python教程中有很多函数,if算是其中之一. 可能最为人所熟知的编程语句就是 if 语句了.例如: >>> >>> ...