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 ...
随机推荐
- 【胡搞的不能AC的题解,暴力搜索一发博弈问题】1995 三子棋 - 51Nod
1995 三子棋 题目来源: syu校赛 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 原题链接: https://www.51nod.com/onlineJudge/ ...
- vue2 自定义键盘事件
- Codeforces Round #597 (Div. 2) B. Restricted RPS
链接: https://codeforces.com/contest/1245/problem/B 题意: Let n be a positive integer. Let a,b,c be nonn ...
- 使用集合方式注入IoC
使用集合方式注入Ioc 1.创建类 //集合 private String[] arrays; //list集合 private List<Integer> lists; //map集合 ...
- vue上传大文件控件
文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹.今天研究了一下这个问题,在 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- [转]C++重载()(强制类型转换运算符)
在 C++ 中,类型的名字(包括类的名字)本身也是一种运算符,即类型强制转换运算符. 类型强制转换运算符是单目运算符,也可以被重载,但只能重载为成员函数,不能重载为全局函数.经过适当重载后,(类型名) ...
- LSTM-航班人数预测
小书匠深度学习LSTM 郑重声明,文章大部分翻译自: Time Series Prediction with LSTM Recurrent Neural Networks in Python with ...
- 7、transformation和action2
一.transformation开发实战 1.map: 将集合中每个元素乘以2 使用map算子,将集合中的每个元素都乘以2 map算子,是对任何类型的RDD,都可以调用的,在Java中,map算子接收 ...
- 贾扬清牛人(zz)
贾扬清加入阿里巴巴后,能否诞生出他的第三个世界级杰作? 文 / 华商韬略 张凌云 本文转载,著作权归原作者所有 贾扬清加入阿里巴巴后,能否诞生出他的第三个世界级杰作? 2017年1月11日,美国 ...