问题描述
假设我们有一个带头结点的单链表来保存单词,每个节点包含一个字符和指向下一个节点的指针。若两个单词的后缀相同,它们可以共享相同的后缀存储空间。例如:
- 单词
loading和being有相同的后缀ing,可以共享存储。
题目要求
设 str1 和 str2 分别指向两个单词所在链表的头节点,请设计一个尽可能高效的算法,找出两个链表共同后缀的起始位置。
要求:
- 给出算法的设计思想。
- 使用 C++ 代码实现算法,并在关键部分给出注释。
- 分析算法的时间复杂度。
设计思路
1. 暴力匹配法
最直接的思路是将链表 str1 的每一个节点依次与链表 str2 的节点进行匹配,如果匹配到相同的节点,则继续向后比较,直到找到第一个完全相同的后缀位置。
实现步骤:
- 从链表
str1开始的每个节点,分别与str2中的节点开始逐一匹配。 - 若匹配的节点数据相同,继续匹配下一个节点;若不相同则跳到下一个节点。
- 当匹配到第一个后缀起始点时,返回该节点位置。
#include <bits/stdc++.h>
using namespace std;
struct Node {
char value;
Node* next;
};
bool check(Node* first, Node* second) {
Node* l = first;
Node* r = second;
while (l != nullptr && r != nullptr) {
if (l->value != r->value)
return false;
l = l->next;
r = r->next;
}
return l == nullptr && r == nullptr;
}
Node* search(Node* first, Node* second) {
Node* x = first;
while (x != nullptr) {
Node* y = second;
while (y != nullptr) {
(x->value == y->value && (x, y)) {
x;
}
y = y->next;
}
x = x->next;
}
;
}
{
Node* common = Node{, Node{, Node{, }}};
Node* str1 = Node{, };
str1->next = Node{, };
str1->next->next = Node{, };
str1->next->next->next = Node{, };
str1->next->next->next->next = common;
Node* str2 = Node{, };
str2->next = Node{, };
str2->next->next = common;
Node* result = (str1, str2);
(result != ) {
cout << << ()result->value << endl;
} {
cout << << endl;
}
;
}


