题目链接:
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;
}
本文介绍了一种高效的算法,用于查找两个字符串的最长公共后缀。通过遍历第一个字符串并标记其节点,在遍历第二个字符串时,一旦遇到已标记的节点即找到了公共后缀的起点。此算法在字符串处理和算法设计领域具有广泛的应用。
1869

被折叠的 条评论
为什么被折叠?



