[PAT] title record

Topic Link

1032 Sharing

The meaning of problems

Given two lists first address, and each node, find the first two lists common node address is not -1

notes

  • Note that when using a static list how to write, how to traverse, in accordance with the meaning of the questions transformation of the structure, such as adding a flag variable here
  • The first way is to traverse a linked list tag for each node is true, then traverse the second, hit the first flag is true that we find nodes
  • Note scanf time %d%c%dis not enough, because the spaces will read at this time! ! Remember written in the original input format %d %c %d, that is, spaces, keeping exactly the same

Code

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6+10;
struct Node{
    char data;
    int next;
    bool flag;
}node[maxn];

int main(){
    int pa,pb,n;
    scanf("%d%d%d",&pa,&pb,&n);
    int add, nxt;
    char ch;
    for(int i=0;i<n;i++){
        scanf("%d %c %d",&add, &ch, &nxt);
        node[add].data = ch;
        node[add].next = nxt;
    }
    while(pa != -1){
        node[pa].flag = true;
        pa = node[pa].next;
    }
    while(pb != -1){
        if(node[pb].flag){
            printf("%05d\n",pb);
            return 0;
        }
        pb = node[pb].next;
    }
    printf("-1\n");
}

Guess you like

Origin www.cnblogs.com/doragd/p/11262366.html
Recommended