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
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct nd{
int rt;
char alfa;
int isFirst;
nd(){
isFirst = ;
}
}node;
node data[];
int main(){
int w1, w2, N;
scanf("%d%d%d", &w1, &w2, &N);
int temp1, temp2;
char c;
for(int i = ; i < N; i++){
scanf("%d %c %d", &temp1, &c, &temp2);
data[temp1].alfa = c;
data[temp1].rt = temp2;
}
int pt = w1;
while(pt != -){
data[pt].isFirst = ;
pt = data[pt].rt;
}
int find = -;
pt = w2;
while(pt != -){
int tag = , pt2 = pt;
if(data[pt].isFirst == ){
while(pt2 != -){
if(data[pt2].isFirst != ){
tag = ;
break;
}
pt2 = data[pt2].rt;
}
if(tag == ){
find = pt;
break;
}
}
pt = data[pt].rt;
}
if(find == -)
printf("-1");
else printf("%05d", find);
cin >> N;
return ;
}

总结:

1、在地址很小的时候,为了方便一般使用静态数组。

A1032. Sharing的更多相关文章

  1. PAT甲级——A1032 Sharing

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

  2. PAT-链表-A1032 Sharing

    题意:给出两条链表的首地址以及若干个节点的的地址.数据.下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1. 思路:使用静态链表,首先遍历一遍第一个链表并进行标记. ...

  3. PAT A1032 Sharing

    题意:给出两条链表的首地址以及若干节点的地址,数据,下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1.思路步骤1:由于地址的范围很小,因此可以直接用静态链表,但是 ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. 伪共享(false sharing),并发编程无声的性能杀手

    在并发编程过程中,我们大部分的焦点都放在如何控制共享变量的访问控制上(代码层面),但是很少人会关注系统硬件及 JVM 底层相关的影响因素.前段时间学习了一个牛X的高性能异步处理框架 Disruptor ...

  6. Salesforce的sharing Rule 不支持Lookup型字段解决方案

    Salesforce 中 sharing rule 并不支持Look up 字段 和 formula 字段.但在实际项目中,有时会需要在sharing rule中直接取Look up型字段的值,解决方 ...

  7. [Erlang 0127] Term sharing in Erlang/OTP 上篇

    之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...

  8. 跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享

    在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...

  9. 006_Salesforce Sharing 使用说明

    Salesforce Sharing 使用说明 背景说明:Salesforce共享实施记录和其它数据时,需要员工之间共享或多个用户在一个组织间的共享.然而,共享这些数据是有风险的,尤其是当它涉及到敏感 ...

随机推荐

  1. 【亲测有效】Github无法访问或者访问速度的解决方案

    我相信,很多朋友都遇到了 Github 访问速度过慢的问题,我也是在此记下笔记,方便以后拿来使用. 第一步.修改Hosts 通过问题的搜索了解到 github 访问很慢一般通过修改 hosts 文件解 ...

  2. Docker网络解决方案 - Calico部署记录

    简单来说,实现docker跨主机容器间通信,常用的第三方网络方案是Flannel,Weave,Calico:Flannel会为每个host分配一个subnet,容器从这个subnet中分配ip,这些i ...

  3. Haproxy基础知识 -运维小结

    开源软件负载均衡器 现在常用的三大开源软件负载均衡器分别是Nginx.LVS.Haproxy. 在之前的文章中已经对比了这三个负载均衡软件, 下面根据自己的理解和使用经验, 再简单说下这三个负载均衡软 ...

  4. eclipse添加maven环境

    一.打开eclipse,选择Window->preference,如下图所示 二.Maven-> installation->add,见下图: 三.选择Directory,选择mav ...

  5. 路由嵌套 active

    http://www.jb51.net/article/102574.htm; https://segmentfault.com/q/1010000008950255 <el-menu :def ...

  6. SE Class's Individual Project--12061161 赵梓皓

    1. 项目预计的用时 其实刚开始以为这个项目不难写,因为上学期oo课程上用java写过类似的程序(貌似还比这个复杂).觉得主要的难点在于学习c++语言. 总的项目被分为大概3个部分. 其一,文件遍历. ...

  7. 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)

    一 . 关于银弹 (Silver Bullet) 银弹,被引申为解决问题的有效办法.IBM大型机之父福瑞德·布鲁克斯在1986年的论文<没有银弹>中表达了他的观点:软件工程中不存在银弹—— ...

  8. Linux 实验一 基础实践

    Linux 实践一 1:软件源的维护方法 删掉DEB打头的 在命令行中输入命令时,可以用命令补全的方法. 下载完成后,使用sudo dpkg-i skype.deb 来完成安装. 2:掌握Linux ...

  9. Android Studio 打包AAR和第三方静态库

    需求 现在有一个第三方库libstatic_add.a和对应的头文件static.h,要求封装一个Module,该Module依赖这个静态库,要求打包的Module包含该静态库. 方案 创建Andro ...

  10. FMDB数据库升级

    FMDBMigrationManager 是与FMDB结合使用的一个第三方,可以记录数据库版本号并对数据库进行数据库升级等操作.首先要集成FMDB和FMDBMigrationManager,建议使用c ...