• A1032 Sharing(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【字符串+结构体+map]


    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
    

    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:

    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

     

    题意分析:

    首先看到这类题第一反应是定义一个结构体存放每个字母的信息,为了不受整数的各种影响,直接全部用字符串处理

    基本思路是首先存放所有字母节点,然后遍历分别找出两个单词的首字母位置,然后定义createWord函数找该单词所有下一个字母位置组成序列

    两个单词全部找到后,从后往前比较找相同的字母,记录最后一次相同字母的位置输出,找不到就输出“-1”。这里为了方便判断两个单词即两个结构体变量是否相同,对==运算符进行函数重载。

    代码如下:

    1. #include
    2. using namespace std;
    3. struct node {
    4. //Address Data Next
    5. string address, data, next;
    6. friend bool operator==(const node& a, const node& b) {
    7. return (a.address == b.address && a.data == b.data && a.next == b.next);
    8. }
    9. };
    10. string w1, w2;
    11. int n;
    12. vector nodelist;
    13. vector word1, word2;
    14. map wordAddr;
    15. vector creatWord(node w)
    16. {
    17. vector word;
    18. node newNode = w;
    19. while (newNode.address!="-1") {
    20. word.push_back(newNode);
    21. newNode = wordAddr[newNode.next];
    22. }
    23. return word;
    24. }
    25. int main()
    26. {
    27. //先存储每个字母
    28. cin >> w1 >> w2 >> n;
    29. nodelist.resize(n);
    30. for (int i = 0; i < n; i++)
    31. {
    32. cin >> nodelist[i].address >> nodelist[i].data >> nodelist[i].next;
    33. wordAddr[nodelist[i].address] = nodelist[i];
    34. }
    35. wordAddr["-1"] = { "-1","-1" ,"-1" };
    36. //找出两个单词的首字母位置,然后找两个单词相同的的下一个字母位置
    37. //转换成将两个单词的所有下一个字母组成数组,然后从后往前比较找相同的
    38. int cnt = 0;
    39. for (int i = 0; i < n; i++)
    40. {
    41. if (cnt == 2)break;
    42. if (nodelist[i].address == w1) {
    43. cnt++;
    44. word1 = creatWord(nodelist[i]);
    45. }
    46. if (nodelist[i].address == w2) {
    47. cnt++;
    48. word2 = creatWord(nodelist[i]);
    49. }
    50. }
    51. //输出两个单词的相同后缀
    52. int len1 = word1.size();
    53. int len2 = word2.size();
    54. string res="-1";
    55. while (len1 != 0 && len2 != 0)
    56. {
    57. if (word1[len1 - 1] == word2[len2 - 1]) {
    58. res = word1[len1 - 1].address;
    59. len1--;
    60. len2--;
    61. }
    62. else break;
    63. }
    64. cout << res << endl;
    65. return 0;
    66. }

    运行结果如下:

     

  • 相关阅读:
    Datart 扩装下载功能之PDF和图片下载
    uboot模式下通过tftp移植内核遇到的问题
    后端关卡18 了解JDBC
    Python文件操作
    haproxy,nginx,keepalived综合运用
    非零基础自学Java (老师:韩顺平) 第13章 常用类 13.2 String 类 && 13.3 字符串的特性 && 13.4 String 类的常见方法
    谈一谈什么是接口测试?怎样做接口测试?
    PDF转Markdown的开源工具解析
    【数据结构-图】有向无环图的应用
    106道Java并发和多线程基础面试题大集合(2w字),这波面试稳了~
  • 原文地址:https://blog.csdn.net/qq_47677800/article/details/126061047