zoukankan      html  css  js  c++  java
  • 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、在地址很小的时候,为了方便一般使用静态数组。

  • 相关阅读:
    通用客户端脚本
    刷一次变一次图的ASP代码
    ASP公共翻页代码
    Web中模态对话框加载后根据加载内容动态改变其大小并使其居中
    创建一个ASP通用分页类
    各大门户网站FLASH广告完全揭密
    插入记录后,获取记录的ID
    ASP实用函数库
    Sametime 8.5.1系统环境要求
    推荐:IBM Lotus Domino 8.5 服务器管理入门手册(适用初学者)
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8531037.html
Copyright © 2011-2022 走看看