1032 Sharing (25)(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 (<= 10^5^), 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

  使用静态链表,设置一个标志位,当该结点出现在第一个链表中时,标志位为true,然后遍历第二个链表,当结点中的标志位为true时,即为寻找的相同后缀的第一个地址

C++代码如下:

 #include<iostream>
#include<cstring>
using namespace std;
#define maxn 100010
struct word {
char ch;
int next;
bool l=false;
}w[maxn]; int main() {
int add1, add2, n;
scanf("%d %d %d", &add1, &add2, &n);
int start, end;
char c;
for (int i = ; i < n; i++) {
scanf("%d %c %d", &start, &c, &end);
w[start].ch = c;
w[start].next = end;
}
for (int i = add1; i != -; i = w[i].next)
w[i].l = true;
int p;
for (p = add2; p != -; p = w[p].next)
if (w[p].l == true) break;
if (p != -) printf("%05d\n", p);
else printf("-1\n");
return ;
}

【PAT】1032 Sharing (25)(25 分)的更多相关文章

  1. PAT 1032 Sharing[hash][链表][一般上]

    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分) 从自信到自闭

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

  3. PAT 1032. Sharing

    其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...

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

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

  5. 1032 Sharing (25分)

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

  6. 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 ...

  7. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  8. PAT 1085 PAT单位排行(25)(映射、集合训练)

    1085 PAT单位排行(25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随 ...

  9. 怎么设置BarTender中二维码大小为25*25

    有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...

随机推荐

  1. Git合并的代码 不提交服务器的方法

    使用Git下载代码的时候,常遇到合并的情况,然后再上传的时候,系统就会自动把合并代码的过程也上传,有时候会感觉非常的烦Merge remote-tracking branch 'choose_remo ...

  2. Go语言?Docker?对新技术怎么看?

    对于 Go 语言和 Docker 这两种技术,在国内的技术圈中有相当大的一部分人和群体还在持观望或是不信任的态度.所以,我想写这篇文章,从两个方面来论述一下我的观点和看法. 上个月,作为 Go 语言的 ...

  3. 【转】汽车CAN总线

    概述 CAN(Controller Area Network)总线协议是由 BOSCH 发明的一种基于消息广播模式的串行通信总线,它起初用于实现汽车内ECU之间可靠的通信,后因其简单实用可靠等特点,而 ...

  4. SP8093 JZPGYZ - Sevenk Love Oimaster 解题报告

    SP8093 JZPGYZ - Sevenk Love Oimaster 题目大意 给定\(n(n\le 10000)\)个模板串,以及\(m(m\le 60000)\)个查询串(模板串总长\(\le ...

  5. Problem A: Dreamweaver 解题报告

    人生首道非传统题给了交互... 考试的时候花了半小时搞清楚了这东西是啥,然后打了10分的暴力...但并没有拿到分. 而且一直不会本地测试... 捣鼓捣鼓了好久,问了人才知道.. 我本地没装g++,下发 ...

  6. valgrind使用指南

    http://note.youdao.com/noteshare?id=5de9c049ccdb1defdc4368db83813dd3

  7. Docker 镜像加速器

      Docker 镜像加速器 我们使用Docker的第一步,应该是获取一个官方的镜像,例如mysql.wordpress,基于这些基础镜像我们可以开发自己个性化的应用.我们可以使用Docker命令行工 ...

  8. 何凯文每日一句打卡||DAY4

  9. git 学习小记之图形化界面客户端

    习惯了 Windows 的用户,一直不喜欢用类似命令行的东西来操作,当然我也不是不喜欢,只是操作太慢了.也许 Linux 大神在命令行的帮助下,办事效率翻倍,那也是非常常见的事情..当然我不是大神,所 ...

  10. lemon spj无效编译器解决方法

    反正我是被坑了很久,心里增的敲难过呀! 我曾经无数次的想把它解决掉: 啊啊啊啊啊啊! 什么嘛!什么嘛! 这个空白的框框里到底要填什么嘛!!! 你已经是一个成熟的lemon了,就不能自动识别给个选项吗! ...