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


    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

    where Address 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 Nextis 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<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 typedef struct nd{
     6     int rt;
     7     char alfa;
     8     int isFirst;
     9     nd(){
    10         isFirst = 0;
    11     }
    12 }node;
    13 node data[100000];
    14 int main(){
    15     int w1, w2, N;
    16     scanf("%d%d%d", &w1, &w2, &N);
    17     int temp1, temp2;
    18     char c;
    19     for(int i = 0; i < N; i++){
    20         scanf("%d %c %d", &temp1, &c, &temp2);
    21         data[temp1].alfa = c;
    22         data[temp1].rt = temp2;
    23     }
    24     int pt = w1;
    25     while(pt != -1){
    26         data[pt].isFirst = 1;
    27         pt = data[pt].rt;
    28     }
    29     int find = -1;
    30     pt = w2;
    31     while(pt != -1){
    32         int tag = 1, pt2 = pt;
    33         if(data[pt].isFirst == 1){
    34             while(pt2 != -1){
    35                 if(data[pt2].isFirst != 1){
    36                     tag = 0;
    37                     break;
    38                 }
    39                 pt2 = data[pt2].rt;
    40             }
    41             if(tag == 1){
    42                 find = pt;
    43                 break;
    44             }
    45         }
    46         pt = data[pt].rt;
    47     }
    48     if(find == -1)
    49         printf("-1");
    50     else printf("%05d", find);
    51     cin >> N;
    52     return 0;
    53 }
    View Code

    总结:

    1、在地址很小的时候,为了方便一般使用静态数组。

  • 相关阅读:
    Python 杨辉三角形
    Python 输出由星号*组成的菱形图案
    Python 计算组合数C(n,i),即从n个元素中任选i个,有多少种选法
    Python 快速判断一个数是不是素数
    判断今天是今年的第几天
    Pyhon 输入若干个成绩,求所有成绩的平均分。每输入一个成绩后询问是 否继续输入下一个成绩,回答“yes”就继续输入下一个成绩,回答“no” 就停止输入成绩
    KMP算法
    递归实现求解幂集问题
    Python 用大量小矩形来画曲线
    Python 所谓的艺术操作2(带颜色)
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8531037.html
Copyright © 2020-2023  润新知