1032 Sharing (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.

1032 Sharing (25 分)  链表  题意_特殊字符

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

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

从后往前比较尾缀,第一个地址不同的结点的下一个结点地址就是结果,可以由于cout<<(it+1)->address,一直有一组数据过不了。。。。%05d呀。竟然卡这么久。不要太不拘小节了,这是本次卡的根本原因   PAT没那么难,千万别怕

既然确定是尾缀才共享,那么从头开始找第一个不同的(总感觉不大对,因为后面的没判断了)竟然也行。测试用例保证了只有尾缀共享的情况

比较依据是地址,绝对不是data字符,相同字符可以有不同地址。

尾缀法:

#include<bits/stdc++.h>
using namespace std;
struct Node{
int address,next;
char data;
}node[100010],a,b;
int h1,h2,n;//防止无用结点
vector<Node> word1,word2;
int main(){
// freopen("in.txt","r",stdin);
cin>>h1>>h2>>n;
int add,next;
char data;
while(n--){
cin>>add>>data>>next;
node[add].address=add;
node[add].next=next;
node[add].data=data;
}
a.address=1e6,b.address=1e7;
word1.push_back(a),word2.push_back(b);//开头分别插入个绝对不相等的特殊字符
for(int i=h1;i!=-1;i=node[i].next) word1.push_back(node[i]);//单词1
for(int i=h2;i!=-1;i=node[i].next) word2.push_back(node[i]);//单词2

vector<Node>::iterator it1=word1.end()-1;
vector<Node>::iterator it2=word2.end()-1;
while(it1->address==it2->address) {it1--,it2--;};//即使完全相同 也会在begin退出 开头插入了特殊字符
if(it1==word1.end()-1||it2==word2.end()-1) cout<<"-1";
else printf("%05d",(it1+1)->address);
return 0;
}

 

找第一个相同的地址法

#include<bits/stdc++.h>
using namespace std;
struct Node{
int address,next;
char data;
}node[100010];
int h1,h2,n,Hash[100010]={0};//防止无用结点
int main(){
// freopen("in.txt","r",stdin);
cin>>h1>>h2>>n;
int add,next;
char data;
while(n--){
cin>>add>>data>>next;
node[add].address=add;
node[add].next=next;
node[add].data=data;
}
for(int i=h1;i!=-1;i=node[i].next){
Hash[i]=1;
}
int i;
for(i=h2;i!=-1;i=node[i].next){
if(Hash[i]==1) break;
}
if(i==-1) cout<<"-1";
else printf("%05d",i);
return 0;
}