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 数据库 SQLException: The user specified as a definer ('root'@'%') does not exist 错误原因及解决办法
The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 经查明:是用户root并没有获 ...
- Git报错:Permission denied (publickey)
Git在克隆的时候报错.Permission denied (publickey). 报错 Permission denied (publickey) 具体如下: 原因:没有将自己的电脑的SSH ke ...
- numpy模块的基本使用
numpy(Numerical Python)提供了python对多维数组对象的支持:ndarray,具有矢量运算能力,快速.节省空间.numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提 ...
- JavaScript 廖
=============== JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中 ======注释 // 以双斜杠开头直到行末 ...
- django Error: HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
# 解决方案: 因自己重新封装user为UserProfile故在 settings中 添加自己的
- Liunx - 命令整理
## Liunx 常用命令 ## ## 注意,在Linux中,文件没有创建时间. 1. ls : 查看当前文件夹下的所有文件 2. mkdir -- 创建一个新的文件夹 - mkdir 参数 文件名 ...
- 后端token认证模板
1.创建一个视图 from rest_framework import exceptions from app01 import models from rest_framework.authenti ...
- 使用 IDEA 打包spring cloud 成 jar在ubuntu 中运行
1. 打开终端 termial , 使用 mvn install . 如果提示 mvn 不是xx 命令 ; 2 则需要配置环境变量 : path : C:\Program F ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part5
Use the Most Robust Selector for Cypress Tests Which selectors your choose for your tests matter, a ...
- HTML 004 属性
HTML 属性 属性是 HTML 元素提供的附加信息. HTML 属性 HTML 元素可以设置属性 属性可以在元素中添加附加信息 属性一般描述于开始标签 属性总是以名称/值对的形式出现,比如:name ...