1032 Sharing (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 (≤), 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 −.

Then N lines follow, each describes a node in the format:

  1. Address Data Next

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

  1. 11111 22222 9
  2. 67890 i 00002
  3. 00010 a 12345
  4. 00003 g -1
  5. 12345 D 67890
  6. 00002 n 00003
  7. 22222 B 23456
  8. 11111 L 00001
  9. 23456 e 67890
  10. 00001 o 00010

Sample Output 1:

  1. 67890

Sample Input 2:

  1. 00001 00002 4
  2. 00001 a 10001
  3. 10001 s -1
  4. 00002 a 10002
  5. 10002 t -1

Sample Output 2:

  1. -1

题意:

求两个链表的首个共同结点的地址。如果没有,就输出-1

错误点:

1.第一次测试点3超时,原因可能是在遍历时,多次出现a[i].v,a[i].nxt,a[i]出现的次数多了那么遍历a的次数也多了,可能会超时。

2.结构体赋值时深度拷贝,要注意!!!深拷贝是将对象及值复制过来,两个对象修改其中任意的值另一个值不会改变。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int v;
  5. }a[];
  6. int main()
  7. {
  8. a[].v=;
  9. node t=a[];
  10. t.v=;
  11. cout<<"a[1].v "<<a[].v<<endl;
  12. cout<<"t.v "<<t.v<<endl;
  13. return ;
  14. }
  15.  
  16. 结果:
  17. a[].v
  18. t.v

AC代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. char k;
  5. int nxt;
  6. int v;
  7. }a[];
  8. int main()
  9. {
  10. int head1,head2,n;
  11. cin>>head1>>head2>>n;
  12. int x,y;char s;
  13. for(int i=;i<=n;i++){
  14. cin>>x>>s>>y;
  15. a[x].k=s;
  16. a[x].nxt=y;
  17. a[x].v=;
  18. }
  19. int f=-;
  20. for(int i=head1;i!=-;i=a[i].nxt){
  21. a[i].v=;
  22. }
  23. for(int i=head2;i!=-;i=a[i].nxt){
  24. if(a[i].v==){
  25. f=i;
  26. break;
  27. }
  28. }
  29. if(f==-){
  30. cout<<f;
  31. }else{
  32. printf("%05d",f);
  33. }
  34. return ;
  35. }

PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)的更多相关文章

  1. 1032 Sharing (25分)

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

  2. 【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 ...

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

  4. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  5. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  6. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  7. 【PAT甲级】1032 Sharing (25 分)

    题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...

  8. PAT 1032 Sharing (25分) 从自信到自闭

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

  9. PAT Advanced 1032 Sharing(25) [链表]

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

随机推荐

  1. 301、404、200、304等HTTP状态

    在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Google无法验证网站几种解决办法,提及到由于 ...

  2. win32通用控件

    1.标准控件 可以在win32窗口程序中添加资源脚本来给程序添加标准控件: 具体操作为:新建资源脚本    ->在.rc文件中添加控件    ->给控件绑定事件:   常用的标准控件:   ...

  3. sql的九个常用语句是什么

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  4. HTML 文字剧中

    HTML 内想使文字剧中的办法  就是 text-align:center 剧中前效果图 剧中后效果图 代码:

  5. Elasticsearch 使用:创建、插入、查询、更新、删除

    Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索 ...

  6. 073_使用 shell 脚本打印如下图形

    #!/bin/bash #打印第一组图片#for(())为类 C 语言的语法格式,也可以使用 for i in;do;done 的格式替换#for((i=1;i<=9;i++))循环会执行 9 ...

  7. hdu 6183

    给出二维平面$opt1.$ 对点 $(x, y)$ 增减颜色 $c$,$opt2.$ 询问矩形 $(1, y_1), (x, y_2)$ 内出现过的颜色种数$x, y <= 1e6, c < ...

  8. 树套树【bzoj3262】陌上花开

    /* [bzoj3262]陌上花开 2014年6月19日1,2430 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的 ...

  9. CF809E 【Surprise me!】

    我们要求的柿子是张这样子的: \[\frac{1}{n * (n - 1)} * \sum_{i = 1}^n\sum_{j = 1}^{n}\phi(a_i*a_j)*dis(i, j)\] 其中\ ...

  10. CSP 2019 退役记

    声明:博主不会时空穿越,也没有造成恐慌,不应禁赛三年 Day0 上午:打板子 Polya定理; exkmp; exbsgs; 乘法逆元; 矩阵快速幂; 扫描线; ST表; excrt; Dirichl ...