1032 Sharing (25 分) java 题解

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

题目描述:

要存储英语单词,一种方法是使用链表,逐个字母地存储单词。为了节省一些空间,如果单词具有相同的后缀,我们可以让它们共享相同的子列表。例如,loading 和 being 的存储方式如图所示。

fig.jpg

你应该找到公共后缀的起始位置(例如,图1中i的位置)。

输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行都包含两个节点地址和一个正N(≤10^{5}),其中两个地址是两个字的第一个节点的地址,N是节点总数。节点地址为5位正整数,空值由−1表示。
然后是N行,每行描述一个节点

Address Data Next

其中Address是节点的位置,data是该节点包含的字母,是选自{a-z,A-Z}的英文字母,Next是下一个节点的位置。

输出规格:
对于每种情况,只需输出公共后缀的5位开始位置。如果这两个单词没有共同的后缀,则改为输出-1。

解题思路:

题目大意为:给定两链表,求两链表尾部后缀相同的首字符(地址也需相同)地址。

先开大数组保存结点的地址,接着通过链表首地址遍历链表,找相同后缀首字符地址。

优化前:在遍历链表时,将两链表结点数据都分别保存到StringBuilder中,遍历builder找相同字符,最后遍历链表找这一字符地址。

缺陷:样例5中包括了同一字符但地址不相同的情况,还需要比较地址相同才行。优化前虽报的超时,但如果不超时就会报答案错误。

优化后:在遍历链表时,将两链表都保存在ArrayList中,通过倒着遍历list来判断字符和地址是否都相同。只会报超时(不超时就会AC)。

模拟测试点5输入数据:

11111 22222 10
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67891
67891 i 00002
00001 o 00010

应输出

00002

优化前java代码:

import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int adress1 = Integer.parseInt(split[0]);
		int adress2 = Integer.parseInt(split[1]);
		int n = Integer.parseInt(split[2]);
		Node1032 node[] = new Node1032[100005];
		for(int i = 0; i < n;i++) {
			split = br.readLine().split(" ");
			int adress = Integer.parseInt(split[0]);
			char data = split[1].charAt(0);
			int next = Integer.parseInt(split[2]);
			node[adress] = new Node1032(adress, data, next);
		}
		int temp = adress1;
		StringBuilder builder = new StringBuilder();
		while(temp != -1) {
			builder.append(node[temp].data);
			temp = node[temp].next;
		}
		temp = adress2;
		StringBuilder builder2 = new StringBuilder();
		while(temp != -1) {
			builder2.append(node[temp].data);
			temp = node[temp].next;
		}

		builder.reverse();
		builder2.reverse();
		
		int i = 0,j = 0;
		boolean flag = true;
		while(i < builder.length() && j < builder2.length()) {
			if(builder.charAt(i) == builder2.charAt(j)) {
				flag = false;
				i++;
				j++;
			}else {
				break;
			}
		}
		if(flag) {
			System.out.println("-1");
			System.exit(0);
		}
		temp = adress1;
		while(temp != -1) {
			if(node[temp].data == builder.charAt(i - 1)) {
				System.out.printf("%05d",node[temp].adress);
			}
			temp = node[temp].next;
		}
	}
}

class Node1032{
	int adress;
	char data;
	int next;
	
	public Node1032(int adress, char data, int next) {
		this.adress = adress;
		this.data = data;
		this.next = next;
	}
	
}

优化后java代码:

import java.io.*;
import java.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int adress1 = Integer.parseInt(split[0]);
		int adress2 = Integer.parseInt(split[1]);
		int n = Integer.parseInt(split[2]);
		Node1032b node[] = new Node1032b[100005];
		for(int i = 0; i < n;i++) {
			split = br.readLine().split(" ");
			int adress = Integer.parseInt(split[0]);
			char data = split[1].charAt(0);
			int next = Integer.parseInt(split[2]);
			node[adress] = new Node1032b(adress, data, next);
		}
		int temp = adress1;
		List<Node1032b> list = new ArrayList<Node1032b>();
		while(temp != -1) {
			list.add(node[temp]);
			temp = node[temp].next;
		}
		temp = adress2;
		List<Node1032b> list1 = new ArrayList<Node1032b>();
		while(temp != -1) {
			list1.add(node[temp]);
			temp = node[temp].next;
		}
		
		int i = list.size() - 1,j = list1.size() - 1;//下标要减1
		boolean flag = true;
		while(i >= 0 && j >= 0) {//已经避免了空串情况
			if(list.get(i).data == list1.get(j).data && list.get(i).adress == list1.get(j).adress) {
				flag = false;
				i--;
				j--;
			}else {
				break;
			}
		}
		if(flag) {
			System.out.println("-1");
			System.exit(0);
		}
		System.out.printf("%05d",list.get(i + 1).adress);
	}
}

class Node1032b{
	int adress;
	char data;
	int next;
	
	public Node1032b(int adress, char data, int next) {
		this.adress = adress;
		this.data = data;
		this.next = next;
	}
	
}

PAT提交截图:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值