1032 Sharing (25 point(s))
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.
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
非常有趣的题。求公共链表。重点是采用nextElem[]记录后继结点的思维。
由于地址都是5位的,用1e5的数组记录它们的后继结点,这样就可以根据题中给出的开始结点,通过访问nextElem[]数组来得出每一条路径,可以用两个vector存储,这样向后回溯,直到找出第一个不相同的结点。
需要注意的是,有可能某个路径是另一个路径的子路径,即不存在第一个不相同的结点。
错误代码(case#5有WA,23分)
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX = 1e5+7;
int nextElem[MAX];
int main(void){
int start1,start2;char c;int N;
cin>>start1>>start2>>N;
int front,rear;
memset(nextElem,-1,sizeof(nextElem));
while(N--){
cin>>front>>c>>rear;
nextElem[front] = rear;
}
vector<int> v1,v2;
while(start1!=-1){
v1.push_back(start1);
start1 = nextElem[start1];
}
while(start2!=-1){
v2.push_back(start2);
start2 = nextElem[start2];
}
int i=v1.size()-1,j=v2.size()-1;
if(i<0||j<0||v1[i]!=v2[j]){//注意
printf("-1\n");
}
else{
for(;i>=0&&j>=0&&v1[i];i--,j--){
if(v1[i]!=v2[j]){
printf("%05d\n",v1[i+1]);
break;
}
}
}
return 0;
}
AC代码:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int MAX = 1e5+7;
int nextElem[MAX];
int main(void){
int start1,start2;char c;int N;
cin>>start1>>start2>>N;
int front,rear;
memset(nextElem,-1,sizeof(nextElem));
while(N--){
cin>>front>>c>>rear;
nextElem[front] = rear;
}
vector<int> v1,v2;
while(start1!=-1){
v1.push_back(start1);
start1 = nextElem[start1];
}
while(start2!=-1){
v2.push_back(start2);
start2 = nextElem[start2];
}
int i=v1.size()-1,j=v2.size()-1;
if(i<0||j<0||v1[i]!=v2[j]){
printf("-1\n");
}
else{
for(;i>=0&&j>=0&&v1[i]==v2[j];i--,j--);
printf("%05d\n",v1[i+1]);
}
return 0;
}
其它思路:
1. 依然用vector形成路径,利用set的去重性质求集合交集,来计算有多少从哪个元素开始重复。
2. 根据第一个链表采用哈希表标记出现过的结点,遍历第二个链表时出现标记即可输出。
大神链接:https://blog.csdn.net/richenyunqi/article/details/79573180