PAT:1032. Sharing (25)

来源:互联网 发布:淘宝用是谁写的 编辑:程序博客网 时间:2024/05/15 23:53

1032. Sharing (25)

时间限制
100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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}, andNext 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 967890 i 0000200010 a 1234500003 g -112345 D 6789000002 n 0000322222 B 2345611111 L 0000123456 e 6789000001 o 00010
Sample Output 1:
67890
Sample Input 2:
00001 00002 400001 a 1000110001 s -100002 a 1000210002 t -1
Sample Output 2:
-1
代码一:
//此解法在acmclub上可以通过,但在pat上有个测试点会超时,但能得到22分(满分25)//因为浙大上机考试喜欢在以前年份题目的基础上变点花样,//所以如果下次是要求我们输出共同的后缀,本解法依然适用#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 100000struct Node { //节点信息结构体int address;char inf;int next;}nodes[N];int add1, add2, add;//add1,add2分别存放第一第二个字符串的首地址char str[N]; //存放第一个字符串的各个字符int addr[N]; //存放第一个字符串各个字符的地址int main(){    //freopen("in.txt","r",stdin);    int n;while(scanf("%d %d %d",&add1,&add2,&n)!=EOF) {int i, j;for(i=0; i<n; i++)scanf("%d %c %d",&nodes[i].address,&nodes[i].inf,&nodes[i].next);int k=0;add = add1;while(add!=-1) { //获取第一个字符串的各个字符和字符对应的地址for(i=0; i<n; i++) {if(nodes[i].address==add) {str[k] = nodes[i].inf; //此句可以不用,但浙大上机考试喜欢在以前年份题目的基础上变点花样,   //所以如果下次是要求我们输出共同的后缀,则可用到addr[k] = add;k++;add = nodes[i].next;break;}}}int ad; //存放公共后缀的首地址int flag=0; //标志两字符串是否有公共的后缀add = add2;while(add!=-1) {for(j=0; j<k; j++) {if(addr[j]==add) { //只有属于公共后缀的字符的地址才会相同,所以只需找到第一个相同的地址即可ad = add;//此处还可可获得相应的下标,以便输出共同后缀str[j]-str[k-1]flag =1;break;}}if(flag)break;else { //若当前地址不跟第一个字符串的所有字符地址相同,则继续寻找for(i=0; i<n; i++) {if(nodes[i].address==add) {add = nodes[i].next;break;}}}}if(flag)printf("%05d\n",ad); //注意输出格式elseprintf("-1\n");}    return 0;}

代码二:(这是参考别人代码的,此题的最优解法)
#include <cstdio>#include <cstring>using namespace std;int  node[100000],temp[100000];int main(){    memset(node,-1,sizeof(node));    int start1,start2,n,from,to;    char s[2];    scanf("%d %d %d",&start1,&start2,&n);    for( int i=0; i<n; i++)    {        scanf("%d %s %d",&from,s,&to);        node[from] = to;    }    while(start1 != -1)    {        temp[start1] = 1;        start1 = node[start1];    }    while(start2 != -1)    {        if(temp[start2])        {            printf("%05d\n",start2);            return 0;        }        start2 = node[start2];    }    printf("-1\n");    return 0;}


1 0