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. 用Python建设企业认证和权限控制平台

    目前大家对Python的了解更多来源是数据分析.AI.运维工具开发,在行业中使用Python进行web开发,同样也是非常受欢迎的,例如:FaceBook,豆瓣,知乎,饿了么等等,本文主要是介绍是利用P ...

  2. 从web启动winform程序

    最近有个客户提出想从网站上启动一个客户端的程序,研究了下,实现方法如下: 1. 注入注册表 try                {                    string appPath ...

  3. 为harbor部署swagger-ui

    有了swagger,所有REST API就一览无余了. Harbor官方已经准备好了部署脚本,部署起来其实特别简单,这里仅作记录. 1.下载部署脚本和swagger文件到本地harbor目录. # c ...

  4. 【LOJ#10131】暗的锁链

    题目大意:给定一个 N 个点无向图的一棵生成树和另外 M 条边,第一次去掉生成树中的一条边,第二次去掉另外 M 条边中的一条边,求有多少种情况可以使得给定的无向图不连通. 题解:首先考虑该生成树,若新 ...

  5. ueditor的上传文件漏洞(c#)

    项目中使用了ueditor,安全测试发现一个漏洞,涉及漏洞的文件名字为UploadHandler.cs,其中有一个方法: private bool CheckFileType(string filen ...

  6. redirect和forward的区别

    异同 1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以 ...

  7. 借读:分布式锁和双写Redis

      本帖最后由 howtodown 于 2016-10-3 16:01 编辑问题导读1.为什么会产生分布式锁?2.使用分布式锁的方法有哪些?3.本文创造的分布式锁的双写Redis框架都包含哪些内容? ...

  8. python+正态分布+蒙特卡洛预测男女身高概率!

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  9. mysql日常笔记(持续更新)

    常用场景 sql_mode问题:http://blog.csdn.net/ccccalculator/article/details/70432123 连续日期补全/数据补零操作 在不使用存储过程和函 ...

  10. centos下安装python3.6.2

    一.下载 官网地址:https://www.python.org/downloads/source/ 我下载的是最新的3.6.2rc版本 # cd /opt/ wget https://www.pyt ...