• 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. }

    运行结果如下:

     

  • 相关阅读:
    BeyondCorp 打造得物零信任安全架构
    记一次神奇的SQL查询经历,group by、order by慢查询优化
    Django-Import-Export插件控制数据导入流程
    Pytorch:Dataset类和DataLoader类
    【vscode】打开多分支同名工程或文件夹便捷管理方法
    PDMS二次开发(二十)——关于1.0.2.0版本升级内容的说明
    Centos7安装Clickhouse单节点部署
    基于Keras版本YOLOV7模型的锂电池自燃预警烟雾检测实践
    手把手教你入门Python中的Web开发框架,干货满满!!
    Rocky linux8.8系统通过packstack安装OpenStack yoga版本
  • 原文地址:https://blog.csdn.net/qq_47677800/article/details/126061047