题目链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920
题目分析:
求两个字符串的公共后缀起始地址。在访问第一个字符串时,将节点标记为true,故当访问到第二个字符串中第一个标记为true的结点便是公共后缀的起始地址。
参考代码:
#include <cstdio>
const int maxn = 100010;
struct node{
char data;
int next;
bool flag;
}list[maxn];
int main()
{
for(int i = 0; i < maxn; i++)
list[i].flag = false;
int l1, l2, n;
scanf("%d %d %d", &l1, &l2, &n);
int address, next;
char data;
for(int i = 0; i < n; i++){
scanf("%d %c %d", &address, &data, &next);
list[address].data = data;
list[address].next = next;
}
int p;
for(p = l1; p != -1; p = list[p].next) //将l1中的结点都标记为true
list[p].flag = true;
for(p = l2; p != -1; p =list[p].next)
if(list[p].flag) break;
if(p != -1){
printf("%05d\n",p);
}else{
printf("-1\n");
}
return 0;
}