PAT 1032. 共享
原题链接
简单
作者:
YAX_AC
,
2024-11-26 20:28:26
,
所有人可见
,
阅读 3
// letter by letter逐个字母 sublist子列表 suffixsuffix share共享
//Figure1图1
//To save some space, we may let the words share the same sublist if they share the same suffix.
//为了节省一些空间,如果单词共享相同的后缀,我们可以让它们共享相同的子列表
//the position of i in Figure1图1中i的位置
//You are supposed to find the starting position of the common suffix
//你需要找到共同后缀的起始位置
#include<iostream>
using namespace std;
const int N = 100010;
int n;
int h1,h2,ne[N],st[N];
char e[N];
int main()
{
cin>>h1>>h2>>n;
for(int i = 0; i<n; i++)
{
int address,next;
char data;
cin>>address>>data>>next;
e[address] = data,ne[address] = next;
}
//遍历链表
for(int i = h1; i!=-1; i = ne[i])
st[i] = true;
for(int i = h2; i!=-1; i = ne[i])
if(st[i])//如果链表1出现过
{
printf("%05d\n",i);
return 0;
}
puts("-1");
return 0;
}