PATA1032题解

某年408真题。。

//
//  main.cpp
//  PATA1032
//
//  Created by Phoenix on 2018/2/8.
//  Copyright © 2018年 Phoenix. All rights reserved.
//

#include 
#include 
const int maxn = 100010;

struct node{
    char data;
    int next;
}node[maxn];

int main(int argc, const char * argv[]) {
    int s1, s2, n;
    scanf("%d %d %d", &s1, &s2, &n);
    for(int i = 0; i < n; i++) {
        int a;
        scanf("%d ", &a);
        scanf("%c %d", &node[a].data, &node[a].next);
    }
    int len1 = 0, len2 = 0;
    int str1 = s1, str2 = s2, temp;
    while(s1 != -1) {
        len1++;
        s1 = node[s1].next;
    }
    while(s2 != -1) {
        len2++;
        s2 = node[s2].next;
    }
    if(len1 < len2) {
        temp = str1; str1 = str2; str2 = temp;
        temp = len1; len1 = len2; len2 = temp;
    }
    while(len1 > len2) {
        str1 = node[str1].next;
        len1--;
    }
    while(len1) {
        if(str1 == str2) {
            printf("%05d", str1);
            break;
        }
        else {
            len1--;
            str1 = node[str1].next;
            str2 = node[str2].next;
        }
    }
    if(len1 == 0) printf("-1");
    return 0;
}


你可能感兴趣的:(PAT)