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.
要存储英语单词,一种方法是使用链表并逐字母存储单词。为了节省一些空间,如果单词共享相同的后缀,我们可以让它们共享相同的子列表。例如,加载和正在被存储,如图1所示。
Figure 1
You are supposed to find the starting position of the common suffix (e.g. the position of i
in Figure 1).
您应该找到公共后缀的起始位置(例如,图1中i的位置)。
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.
每个输入文件包含一个测试用例。对于每种情况,第一行包含两个节点地址和一个正N(≤105),其中这两个地址是两个字的第一个节点的地址,N是节点总数。节点的地址是一个5位数的正整数,NULL由−1表示。
Then N lines follow, each describes a node in the format:
接下来是N行,每行以以下格式描述一个节点:
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.
其中Address是节点的位置,Data是该节点包含的字母,它是从{a-z,a-z}中选择的英文字母,Next是下一个节点的位置。
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.
对于每种情况,只需输出通用后缀的5位数起始位置。如果这两个单词没有通用后缀,请改为输出-1。
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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
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).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;
}
}