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.
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:
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
从后往前比较尾缀,第一个地址不同的结点的下一个结点地址就是结果,可以由于cout<<(it+1)->address,一直有一组数据过不了。。。。%05d呀。竟然卡这么久。不要太不拘小节了,这是本次卡的根本原因 PAT没那么难,千万别怕
既然确定是尾缀才共享,那么从头开始找第一个不同的(总感觉不大对,因为后面的没判断了)竟然也行。测试用例保证了只有尾缀共享的情况
比较依据是地址,绝对不是data字符,相同字符可以有不同地址。
尾缀法:
找第一个相同的地址法