用双指针法巧解——PTA(Advanced Level)1032.Sharing

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.

fig.jpg

Figure 1

You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1).

Input Specification:

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.

Output Specification:

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.

Sample Input 1:

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

Sample Output 1:

67890

Sample Input 2:

00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

Sample Output 2:

-1

思路

  • 题意: 简单来说, 就是在两条链表中找公共部分.
  • 解法
    • 1️⃣比较容易想到的就是用一个map去存储对应的结点, 然后把链表A读取进来, 在扫描链表B的过程中检查有没有同时在map里的结点. 这个算法简单直接, 但是需要额外的存储空间, 还不是很让人满意
    • 2️⃣双指针法
      • 我们先让其中一个指针统计链表A的长度, 让另外一个指针统计链表B的长度. 统计完成后, 重新让指针指向两个链表的开头, 计链表长度之差为dis
      • 让快的指针先走dis的距离, 然后两个再一起出发, 显然, 如果有公共起点, 那么他们一定会相遇, 我们已经消除了两个链表的长度差. 由此我们得到了第一个版本的代码
      • 进一步优化, 设链表A长度为\(a+x\), 链表B长度为\(b+x\), 那么我们如果交替遍历呢(指向链表A的指针跑完链表A的时候马上跑B, 指向B的类似). 用数学公式来推导就是\(a+x+b+x = b+x+a+x\), 对应版本的代码为2
        • \(x=x\), 对应相交的情况
        • \(a+b=b+a\), 对应不相交的情况, 此时刚好停在最后链表尾的位置

代码1

#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;
}

代码2

#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

猜你喜欢

转载自www.cnblogs.com/MartinLwx/p/13209138.html