1032. Sharing (25)

1.题目求两个链表的公共节点。

2.直接建立两个100001长度的数组,一个用来当作链表使用,一个用来当作后面的哈希查询使用。

2.遍历第一个链表,把第一个链表中存在的节点记录在哈希表中。

3.遍历第二个链表,如果哈希中存在某个节点的值,就break,最终输出该节点值。

4.把地址改为int存储,最后输出注意-1和其他的补0的情况。

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

1032sharingFigure 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

where Address 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}, andNext 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

AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

/*
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

11111 22222 9
00012 i 00002
00010 a 12345
00003 g -1
12345 D 00012
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 00012
00001 o 00010
*/
int main(void)
{
	int *m = new int[100001];
	int *exist = new int[100001];
	memset(m, -1, sizeof(m));
	memset(exist, -1, sizeof(exist));
	int sum;
	int a, b;
	scanf("%d %d %d", &a, &b, &sum);
	m[a] = -1;
	m[b] = -1;

	for (int i = 0; i < sum; i++)
	{
		char tmp[10];
		int pre, next;
		scanf("%d %s %d", &pre, tmp, &next);
		m[pre] = next;
	}
	int head = a;
	while (head != -1)
	{//把第一个链表存进哈希表 exist中
		exist[head] = 1;
		head = m[head];
	}
	head = b;
	while (head != -1)
	{
		if (exist[head] == 1)
		{
			break;
		}
		head = m[head];
	}
	if (head != -1)
		printf("%05d\n", head);
	else
		printf("%d\n", head);

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注