PAT (Advanced Level) Practice: 1032 Sharing (25分)

1032 Sharing (25分)

思路

两种方法
一种是遍历一次列表1 ,用set存储,再遍历列表2,如果发现元素存在于set中,输出,结束循环。
第二种是双指针法,两个指针指向两个列表的开头,任何一个指针走到列表结尾就跳到另一个列表的开头,如果地址相同,就输出。

代码

#include <iostream>
#include <cstdio>
using namespace std;
#include <set>

int next1[100000] = { 0 };
char data1[100000] = { 0 };
int main()
{
	int s1, s2, n;
	cin >> s1 >> s2 >> n;
	for (int i = 0; i < n; i++)
	{
		int t1, t2;
		char c;
		cin >> t1 >> c >> t2;
		//scanf_s("%d %c %d", &t1, &c, &t2);
		data1[t1] = c;
		next1[t1] = t2;
	}
	/*方法一:双指针法*/
	/*int i = s1, j = s2;
	while (true)
	{
		if (i == j)
			break;
		else if (i == -1)
		{
			i = s2;
			j = next1[j];
		}
		else if (j == -1)
		{
			j = s1;
		}
		else
		{
			i = next1[i];
			j = next1[j];
		}
	}
	//cout << i;
	if (i == -1)
		cout << i;
	else
		printf("%05d", i);*/

	/*方法二,set集合*/
	set<int> hash1;
	int i = s1;
	while (i != -1)
	{
		hash1.insert(i);
		i = next1[i];
	}
	int flag = 0;
	int j = s2;
	while (j != -1)
	{
		if (hash1.find(j) != hash1.end())
		{
			flag = 1;
			break;
		}
		j = next1[j];
	}
	if (flag == 0)
	{
		cout << "-1";
	}
	else
	{
		printf("%05d", j);
	}
	return 0;
}
posted @ 2021-01-25 19:16  韩天尊  阅读(43)  评论(0)    收藏  举报