1032. Sharing (25)

来源:互联网 发布:下载手机淘宝2017 编辑:程序博客网 时间:2024/05/17 08:55

题目如下
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}, 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
题目分析
建立两个链表,找出两个链表的第一个公共子节点,存在输出第一个公共子节点的首地址,不存在输出-1.


题目难点
1.输入时必须用getchar(),清空换行否则出错。
2.构建两个链表,必须用vector,不能用数组进行哈希,好像内存溢出。。。
3.查找第一个公共子节点时,用map避免重复循环,更加简单。
4.对两个单词的首地址进行判断,有一个不存在,即-1,两个共同后缀就不存在,输出-1.


题目代码如下

#include <iostream>#include <stdio.h>#include <vector>#include <map>/* run this program using the console pauser or add your own getch, system("pause") or input loop */using namespace std;struct node{    int add;    char value;    int next;};int main(int argc, char *argv[]) {    int add1,add2,n;    scanf("%d%d%d",&add1,&add2,&n);    if(add1==-1 || add2==-1){//一个单词不存在,肯定没有后缀         printf("-1\n");        return 0;    }    node mynode[100000];    for(int i=0;i<n;i++){        int add,next;        char value;        scanf("%d",&add);        getchar();//清除最后的空格和换行,必不可少         scanf("%c",&value);        scanf("%d",&next);        mynode[add].add=add;        mynode[add].value=value;        mynode[add].next=next;    }    vector<node> list1;//第一个单词     vector<node> list2;//第二个单词     map<int,int> map1;//第一个单词的每个字符的首地址     map<int,int> map2;//第二个单词的每个字符的首地址     while(add1!=-1){//查找第一个单词         node t = mynode[add1];        list1.push_back(t);        if(map1.count(t.add)==0){            map1[t.add]=t.add;        }        add1 = t.next;    }    while(add2!=-1){//查找第二个单词         node t = mynode[add2];        if(map2.count(t.add)==0){            map2[t.add]=t.add;        }        list2.push_back(t);        add2 = t.next;    }    int size1 = list1.size();    int size2 = list2.size();    int size= -1;    bool flag = false;    if(size1>size2){//谁长从谁开始找         size = size1;        for(int i=0;i<size;i++){            node t = list1[i];            int k = t.add;            if(map1.count(k)==1 && map2.count(k)==1){//都存在的                 printf("%05d",t.add);                flag = true;                break;//必须有,因为公共后缀的字符可能有多个             }        }    }    else{        size = size2;        for(int i=0;i<size;i++){            node t = list2[i];            int k = t.add;            if(map1.count(k)==1 && map2.count(k)==1){                printf("%05d\n",t.add);                flag = true;                break;            }        }    }    if(!flag){//不存在公共后缀         printf("-1\n");    }    return 0;}