版本2
静态链表
#include <cstdio>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cmath>
using namespace std;
const int N = 1e5+5;
struct node{
char data;
int address, next, idx;
}E[N];
int main(){
int stA, stB, n, add, next;
char c;
cin>>stA>>stB>>n;
for(int i = 0; i < n; i++){
cin>>add>>c>>next;
E[add]= {c, add, next, 0};
}
while(stA != -1){
E[stA].idx = 1;
stA = E[stA].next;
}
bool flag = false;
while(stB != -1){
if(E[stB].idx == 1){
printf("%05d", E[stB].address);
flag = true;
break;
}
stB = E[stB].next;
}
if(!flag) printf("-1");
return 0;
}
版本1
思路:使用静态链表,相当于哈希操作。
注意:地址需要输出5位数
#include<cstdio>
struct node{
char c;
int next;
int index;
}Node[100006];
int main(){
int x,y,n,cur,next;
char c;
scanf("%d%d%d",&x,&y,&n);
for(int i=0;i<n;i++){
scanf("%d %c %d",&cur,&c,&next);
Node[cur].c=c;
Node[cur].next=next;
Node[cur].index=0;
}
while(x!=-1){
Node[x].index=1;
x=Node[x].next;
}
int flag=0;
while(y!=-1){
if(Node[y].index==1){
flag=y;
break;
}
y=Node[y].next;
}
if(flag==0)
printf("-1\n");
else
printf("%05d\n",flag);
return 0;
}