PAT 甲级 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 分)(结构体模拟链表,结构体的赋值是深拷贝)的更多相关文章
- 1032 Sharing (25分)
1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...
- 【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 ...
- 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 ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT甲级】1032 Sharing (25 分)
题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...
- PAT 1032 Sharing (25分) 从自信到自闭
题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...
- 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 ...
随机推荐
- Mysql DELETE 不能使用别名? 是我不会用!
今天碰到一个sql问题,就是在delete中加了别名,导致报错了:"[Err] 1064 - You have an error in your SQL syntax; ..." ...
- linux其他
1.安装上传下载指令 sz/rz yum install -y lrzsz 2.flask+gunicorn 代码更新升级部署 ps -ef | grep gunicorn 获取master进程 ki ...
- JsonObject常用转换
我们在平时的开发中,com.alibaba.fastjson.JSONObject是经常会用到的JSON工具包,同样它的转换方法也会经常被我们使用,包括对象转成JSON串,JSON串转成java对象等 ...
- Debian 9.x "stretch" 解决 /etc/rc.local 开机启动问题
由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local 文件,但是 Debian 9 默认不带 /etc/rc.local 文件,而 rc. ...
- MongoDB 集合与文档操作
一.创建DB 1.查看DB >show dbs 2.创建DB >use mydb 3.查看当前DB >db 4.删除DB >use mydb >db.dropDataba ...
- 代码 | 用ALNS框架求解一个TSP问题 - 代码详解
写在前面 前面好多篇文章,我们总算是把整个ALNS的代码框架给大家说明白了.不知道大家对整个框架了解了没有.不过打铁要趁热,心急了要吃热豆腐.今天就来实战一下,教大家怎么用ALNS的代码框架,求解一个 ...
- 【原创】go语言学习(九)指针类型
目录 变量和内存地址 指针类型 值拷贝和引用拷贝 变量和内存地址 1.每个变量都有内存地址,可以说通过变量来操作对应大小的内存 var a int32 a = 100 fmt.Printf(“%d\n ...
- gulp4配置多页面项目编译打包
又开始公司的新项目了... 那当我们拿到公司新项目的时候我们需要做些什么呢? 下面就来分享一下我的工作步骤吧(仅使用于初学者,大神勿见怪- -,有不好的地方希望指出,十分感谢) 1. 整版浏览 这是一 ...
- (5)打造简单OS-进入保护模式
1.简介: 8086实模式 80286才出现保护模式,保护模式下的段寄存器存储的是段选择子,不在是8086实模式的段基址了! [扩展知识]:可以看了后面知识在回头看一下这段. [8086各个段寄存器和 ...
- mybatis oracle 逆向工程