1032. Sharing (25)

时间限制
100 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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


提交代码

1.输出注意格式!!

2.不要投机!!

方法一:

首先从第一个链表头开始遍历链表1, 对每个节点进行标记。然后从第二个链表头开始遍历链表2, 当碰到一个节点是已经被标记了的, 那么这个节点就是第一个公共节点。

 1 #include<cstdio>
 2 #include<stack>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 using namespace std;
 9 int mem[100005];
10 bool vis[100005];
11 int main(){
12     //freopen("D:\\INPUT.txt","r",stdin);
13     int fa,fb,n;
14     scanf("%d %d %d",&fa,&fb,&n);
15     int i,point;
16     char c;
17     for(i=0;i<n;i++){
18         scanf("%d",&point);
19         cin>>c;
20         scanf("%d",&mem[point]);
21     }
22     int pa=fa,pb=fb;
23     while(pa!=-1){
24         vis[pa]=true;
25         pa=mem[pa];
26     }
27     while(pb!=-1){
28         if(vis[pb]){
29             break;
30         }
31         pb=mem[pb];
32     }
33     if(pb!=-1){
34         printf("%05d\n",pb);
35     }
36     else{
37         printf("-1\n");
38     }
39     return 0;
40 }

方法二:模拟链表法

 1 #include<cstdio>
 2 #include<stack>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 using namespace std;
 9 int mem[100005];
10 int main(){
11     //freopen("D:\\INPUT.txt","r",stdin);
12     int fa,anum=0,fb,bnum=0,n;
13     scanf("%d %d %d",&fa,&fb,&n);
14     int i,point;
15     char c;
16     for(i=0;i<n;i++){
17         scanf("%d",&point);
18         cin>>c;
19         scanf("%d",&mem[point]);
20     }
21     int pa=fa,pb=fb;
22     while(pa!=-1){
23         anum++;
24         pa=mem[pa];
25     }
26     while(pb!=-1){
27         bnum++;
28         pb=mem[pb];
29     }
30     pa=fa,pb=fb;
31     while(anum>bnum){
32         anum--;
33         pa=mem[pa];
34     }
35     while(anum<bnum){
36         bnum--;
37         pb=mem[pb];
38     }
39     while(pa!=pb){
40         pa=mem[pa];
41         pb=mem[pb];
42     }
43     if(pa!=-1){
44         printf("%05d\n",pa);
45     }
46     else{
47         printf("-1\n");
48     }
49     return 0;
50 }

方法三:

统计被指向节点,如果一个节点被指向2次,那么这个点就是公共开始点,或者-1出现2次。但由于测试样例的设计恰好避免了这种投机取巧。。。。此方法不能拿全分!!

 1 #include<cstdio>
 2 #include<stack>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 using namespace std;
 9 set<int> ha;
10 int main(){
11     //freopen("D:\\INPUT.txt","r",stdin);
12     int fa,fb,n,fin;
13     scanf("%d %d %d",&fa,&fb,&n);
14     int i,point;
15     char c;
16     for(i=0;i<n;i++){
17         scanf("%d",&point);
18         cin>>c;
19         scanf("%d",&point);
20         if(ha.count(point)==1){//第二次找到point
21             fin=point;
22         }
23         ha.insert(point);
24     }
25     if(fin!=-1){
26         printf("%05d\n",fin);
27     }
28     else{
29         printf("-1\n");
30     }
31     return 0;
32 }

转载于:https://www.cnblogs.com/Deribs4/p/4777324.html

pat1032. Sharing (25)相关推荐

  1. PAT甲级 1032 Sharing (25分) 测试点5陷阱

    题目 1032 Sharing 分析 suffix是后缀,题目的意思是求两个单词的公共后缀的第一个字符的地址.我看有些博客说求的是首个共用结点的地址,我觉得是不对的. 晴神/柳神的解法,是把第一个单词 ...

  2. 1032 Sharing (25 分) 【难度: 一般 / 知识点: 链表】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 测试点3:-1 a -1 这种情况 统计第一 ...

  3. 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...

  4. PAT 1003 Sharing (25)

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

  5. PAT:1032. Sharing (25) AC

    #include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char data; i ...

  6. 1032. Sharing (25)-PAT甲级真题

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

  7. 浙江大学PAT_甲级_1032. Sharing (25)

    题目地址:点击打开链接 To store English words, one method is to use linked lists and store a word letter by let ...

  8. CV:翻译并解读2019《A Survey of the Recent Architectures of Deep Convolutional Neural Networks》第一章~第三章

    CV:翻译并解读2019<A Survey of the Recent Architectures of Deep Convolutional Neural Networks>第一章~第三 ...

  9. PAT甲级题目翻译+答案 AcWing(链表)

    1032 Sharing (25 分) 题意 : suffix后缀:prefix前缀 每个结点存一个字母,一共存两个单词 分别给两个单词的第一个字母的地址以及总共的结点数 给出所有结点的地址数值和下一 ...

  10. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

最新文章

  1. 向基于Linux的Oracle RAC 10g集群添加新节点
  2. oracle创建表分区表,oracle创建分区表
  3. Can't read [proguard.ClassPathEntry@1a0c10f] (No such file or directory)
  4. VMware安装MacOSx系统
  5. MSSQL字符串处理-清除指定不连续或连续的字符
  6. maven 多模块项目关系
  7. hazelcast_HazelCast的Spring-Boot和Cache抽象
  8. java nginx 例子_Java及nginx实现文件权限控制代码实例
  9. 电脑屏保在哪里设置_手机屏保调成绿色能护眼?真的吗?
  10. 1290. 二进制链表转整数
  11. 【Elasticsearch】es 集群健康值 红色 red 分片 未分配
  12. mysql 导出bson格式_mongodb 导入导出GridFS【图片/文件/视频/音频等多媒体文件的导入导出】...
  13. jQuery 省市区多级(三级/四级/五级。。。)联动 BY 凨来了
  14. Inno Setup 软件安装包制作
  15. 常用数据分析方法总结
  16. Python-OpenCV——Image inverting
  17. c语言闰月的计算方法,如何计算闰月如何用计算机编程? 爱问知识人
  18. 创建自己的腾讯云存储桶,将图片上传到腾讯云,并实现父子之间的数据双向绑定
  19. java微信支付超时_Java语言:微信支付之关闭订单
  20. MBA提前面试:商学院看重软实力

热门文章

  1. Redis Desktop Manager for Mac(Redis桌面管理工具)
  2. 利用zabbix监控ogg进程(Windows平台下)
  3. Python之路第二天
  4. JS 同步本地时间和服务器时间
  5. 最流行的国家级域名是什么?不是.cn 也不是.uk
  6. PostgreSQL在何处处理 sql查询之八
  7. perl Makefile.PL;make;make install 安装时报错
  8. Eucalyptus-NC管理
  9. 利用OFBiz实现Single Sign On单点登录
  10. 终于找全啦!一二线城市知名互联网公司名单!对着找就对了...