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. 防止重复提交demo

    利用session防止重复提交 思路: 前端控制:在点击提交按钮后设置按钮不可用. 后台控制:利用session,在初次进入表单页面的时候前生成一个随机token,将token保存到session并返 ...

  2. NumsCount

    package com.home.test; import java.util.Arrays; public class NumsCount { public void getNumCount(int ...

  3. github个人作业

    信息学院本科生课程设计 题目                    文件加密和解密 课程名称 面向对象程序设计课程设计 课程编号 X031749 所在专业 计算机科学与技术 所在班级 计科高职13-3 ...

  4. 嵌入式linux教程

    串口通信minicom $ sudo apt-get install minicom ///安装 # minicom –s //运行 //CTRL+A Z 弹出菜单       2.NFS网络文件配置 ...

  5. Sprint冲刺第二阶段之6---10天(下)

    11月24号——12月8号,这一个时间段学校的电压不是很稳定,时不时会断电,为了冲刺的完整性,我们商量决定把这一时间段做的事情写成一个连贯的小日记.然后统一在整个时间段一起发出来. 经过一个阶段的努力 ...

  6. MCMC等采样算法

    一.直接采样 直接采样的思想是,通过对均匀分布采样,实现对任意分布的采样.因为均匀分布采样好猜,我们想要的分布采样不好采,那就采取一定的策略通过简单采取求复杂采样. 假设y服从某项分布p(y),其累积 ...

  7. 剑指offer:滑动窗口的最大值

    滑动窗口的最大值 题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值 ...

  8. git 快捷键

    实际上就是弄了个别名 $ git config --global alias.st status $ git config --global alias.ci commit $ git config ...

  9. mxnet,theano与torch的简单比较

    这篇文章我想来比较一下Theano和mxnet,Torch(Torch基本没用过,所以只能说一些直观的感觉).我主要从以下几个方面来计较它们: 1.学习框架的成本,接口设计等易用性方面. 三个框架的学 ...

  10. php技术–php中感叹号!和双感叹号!!的用法(三元运算)

    ---恢复内容开始--- if(文章==有用){狂点我;} 在php持术或其他语言中我们经常会看到感叹号的用法,有一定程序语言基础的朋友都知道单个感叹号的作用是取反的意思,也就是取当前结果的反面,如: ...