PAT 1032 Sharing (25)

 

1032 Sharing (25)(25 分)

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.

            Snip20160807_66

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 (<= 10^5^), 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}, 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

乍一看是链表题,可是仔细一想,100ms 的时间限制,顿时觉得肯定是用hash了。

这道题的意思是找两个单词的后缀,并返回第一个地址,有几个点在做题时遇到的:

1:题目中说是给两条链表,是不是所有数据项都是链表的数据项,会不会存在有一个链节点是不在两条链表之中的?

2:这个后缀,到底是什么?在中间出现相同字符串算后缀吗?还是题目保证后缀出现即在最后。

根据第一条:我事先尝试了用hash,思路很简单:统计hash[Next]++,意思是每个Next的指向(出度)都记录下来。当第一个出现2 就意味着有两条链指向了该节点,这后缀要出现,必是从该节点开始,输出即可。

	for(int i=0;i<N;i++){
		scanf("%d %c %d",&Address,&Data,&Next);
		//mapR[Address] = Data;
		list[Address] = Next;
		_hashC[Next]++;
	}
	int p = beginA1;
	while(p!=-1){
		if(_hashC[p]==2){
			printf("%05d",p);
			return 0;
		}
		p=list[p];
	}

意图很简单。但是只得了12分。 那么就意味着中间的节点有些是冗余节点。

那么随机有第二个想法:两条链表依次遍历,第一个先遍历,第二个再遍历,在第一个遍历时遇到一个Node,_hashC[p]++;

当第二次遍历时,只要_hashC[p]++后值为2,那么该点即为答案。

AC代码:

#include<stdio.h>
#include<map>
#include<vector>
int _hashC[100000]={0};
int list[100000]={0};
using namespace std;
struct Node{
	int Address;
	char Data;
	int Next;
	Node(){this->Address = -1;this->Data =' ';this->Next=-1;}
};
int main(){
	int beginA1,beginA2,N,Address,Next;
	char Data; 
	scanf("%d%d",&beginA1,&beginA2);
	scanf("%d",&N);
	Node tempNode;
	for(int i=0;i<N;i++){
		scanf("%d %c %d",&Address,&Data,&Next);
		list[Address] = Next;
	}
	int p = beginA1;
	while(p!=-1){
		_hashC[p]++;
		p = list[p];
	}
	p = beginA2;
	while(p!=-1){
		_hashC[p]++;
		if(_hashC[p]==2){
			printf("%05d",p);
			return 0;
		}
		p = list[p];
	}
	printf("-1");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值