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:

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:

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

错误点:

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

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

#include<bits/stdc++.h>
using namespace std;
struct node{
int v;
}a[];
int main()
{
a[].v=;
node t=a[];
t.v=;
cout<<"a[1].v "<<a[].v<<endl;
cout<<"t.v "<<t.v<<endl;
return ;
} 结果:
a[].v
t.v

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
char k;
int nxt;
int v;
}a[];
int main()
{
int head1,head2,n;
cin>>head1>>head2>>n;
int x,y;char s;
for(int i=;i<=n;i++){
cin>>x>>s>>y;
a[x].k=s;
a[x].nxt=y;
a[x].v=;
}
int f=-;
for(int i=head1;i!=-;i=a[i].nxt){
a[i].v=;
}
for(int i=head2;i!=-;i=a[i].nxt){
if(a[i].v==){
f=i;
break;
}
}
if(f==-){
cout<<f;
}else{
printf("%05d",f);
}
return ;
}

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. 第七届蓝桥杯C/C++程序设计本科B组决赛 ——凑平方数(填空题)

    凑平方数 把0~9这10个数字,分成多个组,每个组恰好是一个平方数,这是能够办到的.比如:0, 36, 5948721 再比如:10985247361, 25, 63907840, 4, 289, 1 ...

  2. web server

    嵌入式的:boa  thttpd wn 网站的: iis apache tomcat ngnix python 的  ruby的 自定的 windows  w3mfc c#自定义的1对1 webser ...

  3. 我的 archlinux 内核参数配置

    title Arch Linux linux /vmlinuz-linux initrd /amd-ucode.img initrd /initramfs-linux.img options root ...

  4. Flask+nginx+Gunicorn部署

    当我们开发完Flask项目后是不能直接通过命令启动服务来使用的(扛不住的) Gunicorn 是一个给 UNIX 用的 WSGI HTTP 服务器.这是一个从 Ruby 的 Unicorn 项目移植的 ...

  5. yum安装mysql(指定版)

    首先需要删除已经存在的mysql,不然后面会报错: 快速删除: yum remove mysql mysql-server mysql-libs mysql-server 查找残余文件: rpm -q ...

  6. java利用webuploader实现超大文件分片上传、断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  7. 使用Python+selenium实现第一个自动化测试脚本

    原blog 一,安装Python. python官方下载地址:https://www.python.org/downloads/ 安装后点击开始菜单,在菜单最上面能找到IDLE. IDLE是pytho ...

  8. 初识QuartusII 9.0(破解,半加器的仿真,综合:上)

    由于在意大利期间,用的xilinx公司的ZYBO板子,相应的软件用ISE,SDK.回国买了altera公司的板子,自然也要学习国内较流行的软件(TB大西瓜家,因此相关例程也是大部分引用他家).Quar ...

  9. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  10. tesonflow实现word2Vec

    word2Vec 是实现从原始语料中学习字词空间向量的预测模型 使用word2Vec的skip_Gram模型 import collections import math import os impo ...