美文网首页
PAT 甲级 刷题日记|A 1032 Sharing (25 分

PAT 甲级 刷题日记|A 1032 Sharing (25 分

作者: 九除以三还是三哦 | 来源:发表于2021-08-05 21:01 被阅读0次

    单词积累

    • suffix 后缀 词尾
    • sublist 子表 分表

    题目

    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:

    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
    结尾无空行
    

    思路

    刚开始被链表的形式吓到,不会写链表,采取了取巧的方式,判断输入的第三列,哪个数字出现了两次,就说明从该位置处开始共享。这样做居然也可以对大部分样例,很不错。

    正确完善的思路应该是使用静态链表的方法,其实就是结构体数组,去模拟遍历。遍历的方式很新奇,马住。

    代码1

    #include <bits/stdc++.h>
    using namespace std;
    map<int,int> num;
    
    int main() {
        int a;
        int b = 0;
        char c;
        int start1, start2, nodes;
        cin>>start1>>start2>>nodes;
        int res = 0;
        while (nodes--) {
            cin>>a>>c>>b;
            num[b]++;
            if(num[b] == 2) res = b;
        }
        if (start1 == start2) printf("%05d", start1);
        else if (res == -1) printf("%d", res);
        else {
            printf("%05d", res);
        }
    }
    

    代码2

    #include <bits/stdc++.h>
    using namespace std;
    
    struct Node{
        char c;
        int next;
        int flag;
    }node[100010];
    
    int main() {
        int start1, start2, N;
        cin>>start1>>start2>>N;
        int address;
        for (int i = 0; i < N; i++) {
            cin>>address;
            cin>>node[address].c>>node[address].next;
            node[address].flag = 0;
        }
        int p;
        for (p = start1; p != -1; p = node[p].next ) {
            node[p].flag = 1;
        }
        for (p = start2; p != -1; p = node[p].next ) {
            if (node[p].flag == 1) {
                break;
            }
        }
        if (p != -1) {
            printf("%05d", p);
        } else {
            printf("%d", p);
        }
    }
    

    相关文章

      网友评论

          本文标题:PAT 甲级 刷题日记|A 1032 Sharing (25 分

          本文链接:https://www.haomeiwen.com/subject/wctvvltx.html