zoukankan      html  css  js  c++  java
  • 1032 Sharing

    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.

    fig.jpg

    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:

    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:

    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

    题意:

      用链表来表示两个英文单词,如果两个链表的后缀相同,则可以被两个单词共用。求出相同后缀的第一个单词的Address。

    思路:

      模拟。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     int s1, s2, n;
     7     cin >> s1 >> s2 >> n;
     8     map<int, int> m;
     9     int address, next;
    10     char data;
    11     for (int i = 0; i < n; ++i) {
    12         cin >> address >> data >> next;
    13         m[address] = next;
    14     }
    15     int len1 = 0, len2 = 0;
    16     int temp1 = s1, temp2 = s2;
    17     while (temp1 != -1) {
    18         len1++;
    19         temp1 = m[temp1];
    20     }
    21     while (temp2 != -1) {
    22         len2++;
    23         temp2 = m[temp2];
    24     }
    25     while (len1 != len2) {
    26         if (len1 > len2) {
    27             len1--;
    28             s1 = m[s1];
    29         } else {
    30             len2--;
    31             s2 = m[s2];
    32         }
    33     }
    34     bool found = false;
    35     while (len1 > 0) {
    36         if (s1 == s2) {
    37             found = true;
    38             cout << setw(5) << setfill('0') << s1 << endl;
    39             break;
    40         } else {
    41             s1 = m[s1];
    42             s2 = m[s2];
    43             len1--;
    44         }
    45     }
    46     if (!found) cout << "-1" << endl;
    47     return 0;
    48 }

    最后一组数据刚开始提交的时候超时了,但是提交了几次之后竟然通过了,搞不懂。

  • 相关阅读:
    子串周期查询问题的相关算法及其应用(原文为2019年国家集训队论文集)
    微软最有价值专家 Azure DevOps MVP(第六年)
    当一个程序员一天被打扰 10 次, 后果很惊人
    什么是CAP定理?
    Java中的锁原理、锁优化、CAS、AQS详解
    如何停止一个正在运行的线程?
    lammps总结(7.27-7.30)
    packmol建模
    Linux 命令 (1)
    ElementUI中的el-select中多选回显数据后没法重新选择和更改
  • 原文地址:https://www.cnblogs.com/h-hkai/p/13125560.html
Copyright © 2011-2022 走看看