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 Nextis 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

题目大意:给定两个整数作为起始地址,然后输入N行数据,数据格式为(地址 字符 下一跳),三元组组成。求从两条链的后缀相同的起始点地址。
解题思路:直接开辟长度为10^5的数组来存储地址即可。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
int L[100002];
int flag[100002];
int f1,f2,n;
memset(L,-1,sizeof(L));
memset(flag,0,sizeof(flag));
scanf("%d%d%d",&f1,&f2,&n);
int i,j,index,value;
char c;
for(i=0;i<n;i++){
scanf("%d %c %d",&index,&c,&value);
L[index]=value;
}
for(i=f1;i!=-1;i=L[i]){
flag[i]=1;
}
for(i=f2;i!=-1;i=L[i]){
if(flag[i]==1){
break;
}
}
if(i==-1){
printf("-1\n");
}
else{
printf("%05d\n",i);
}
return 0;
}

  

												

1032. Sharing (25)的更多相关文章

  1. 【PAT】1032 Sharing (25)(25 分)

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  2. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  3. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  4. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  5. 1032. Sharing (25) -set运用

    题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...

  6. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  7. 1032 Sharing (25)(25 point(s))

    problem To store English words, one method is to use linked lists and store a word letter by letter. ...

  8. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

  9. 【PAT甲级】1032 Sharing (25 分)

    题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...

随机推荐

  1. 关于<T> T[] toArray(T[] a) 方法

    http://mopishv0.blog.163.com/blog/static/5445593220101016102129741/ private List<String> uploa ...

  2. slf4j的java包冲突问题

  3. selenium之下载

    # 测试下载功能,保存文件到指定的目录 # 不同的浏览器配置是不同的,本例使用chrome浏览器 # author:gongxr # date:2017-07-25 import time from ...

  4. Classification with DeepLearning

    分类网络总结 https://github.com/handong1587/handong1587.github.io/blob/master/_posts/deep_learning/2015-10 ...

  5. std::string 字符串替换

    std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...

  6. 数据库左右连接on后的限制条件问题

    测试环境: MySQL 5.7.19 HeidiSQL 9.3 数据库界面连接工具(挺好用的) 碰到的问题是: Select * from t1 left outer join t2 on t1.id ...

  7. day27 封装

    广义上的面向对象的封装: 代码的保护,面向对象思想本身就是一种封装 只让自己的对象调用自己类的方法 狭义的封装: 面向对象的三大特性之一,其他两个是继承和多态. 属性和方法都可以藏起来不让你看到 cl ...

  8. 搜索Ex

    哎呀好几天没写POI题解了 (>﹏<) 看着摇曳不定的小旗子深深惶恐 打算开始肝洛谷试炼场的提高分区了[对我就是这么菜… 搜索Ex 比暴搜不错得多的题 洛谷P1514 引水入城 拆成两问来 ...

  9. Linux 系统免密码登陆远程服务器

    在当前Linux生成公钥私钥ssh-keygen公钥私钥存放路径为:~/root/.ssh/id_rsa id_rsa.pub 登陆被远程主机,进入目录~/root/.ssh/是否存在authoriz ...

  10. 【刷题】AtCoder Regular Contest 001

    A.センター採点 题意:给一个只包含1.2.3.4的字符串,求出现次数最多和最少的字符 做法:还能怎么做... #include<bits/stdc++.h> #define ui uns ...