To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading
and being
are stored as showed in Figure 1.
Figure 1
You are supposed to find the starting position of the common suffix (e.g. the position of i
in Figure 1).
Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by ?1.
Then N lines follow, each describes a node in the format:
Address Data Next
whereAddress
is the position of the node, Data
is the letter contained by this node which is an English letter chosen from { a-z, A-Z }, and Next
is the position of the next node.
For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output -1
instead.
11111 22222 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010
67890
00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1
-1
map
去存储对应的结点, 然后把链表A读取进来, 在扫描链表B的过程中检查有没有同时在map
里的结点. 这个算法简单直接, 但是需要额外的存储空间, 还不是很让人满意dis
dis
的距离, 然后两个再一起出发, 显然, 如果有公共起点, 那么他们一定会相遇, 我们已经消除了两个链表的长度差. 由此我们得到了第一个版本的代码#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
struct node
{
int address;
char data;
int next;
}LinkList[MAXN];
int main()
{
int f1, f2, n;
scanf("%d%d%d", &f1, &f2, &n);
int address, next;
char data;
while(n--)
{
scanf("%d %c %d", &address, &data, &next);
LinkList[address].address = address;
LinkList[address].data = data;
LinkList[address].next = next;
}
int cur1 = f1, cur2 = f2;
int l1 = 0, l2 = 0;
while(cur1 != -1)
{
l1++;
cur1 = LinkList[cur1].next;
}
while(cur2 != -1)
{
l2++;
cur2 = LinkList[cur2].next;
}
int dis;
if(l1 < l2) //始终让cur1指向更长的部分
{
cur1 = f2;
cur2 = f1;
dis = l2 - l1;
}else{
cur1 = f1;
cur2 = f2;
dis = l1 - l2;
}
while(dis--) cur1 = LinkList[cur1].next; //统一起始位置
while(cur1 != -1 && cur2 != -1)
{
if(cur1 == cur2)
{
printf("%05d", LinkList[cur1].address);
return 0;
}
cur1 = LinkList[cur1].next;
cur2 = LinkList[cur2].next;
}
cout << -1;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
struct node
{
int address;
char data;
int next;
}LinkList[MAXN];
int main()
{
int f1, f2, n;
scanf("%d%d%d", &f1, &f2, &n);
int address, next;
char data;
while(n--)
{
scanf("%d %c %d", &address, &data, &next);
LinkList[address].address = address;
LinkList[address].data = data;
LinkList[address].next = next;
}
int cur1 = f1, cur2 = f2;
while(cur1 != cur2)
{
//cout << cur1 << " " << cur2 << endl;
cur1 = cur1 == -1 ? f2 : LinkList[cur1].next;
cur2 = cur2 == -1 ? f1 : LinkList[cur2].next;
}
if(cur1 == -1)
cout << -1;
else
printf("%05d", cur1);
return 0;
}
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920
用双指针法巧解——PTA(Advanced Level)1032.Sharing
原文:https://www.cnblogs.com/MartinLwx/p/13209138.html