PAT甲级专题|链表
PAT链表专题
关于PAT甲级的链表问题,主要内容 就是”建立链表“
所以第一步学会模拟链表,pat又不卡时间,这里用vector + 结构体,更简洁
模拟链表的普遍代码
const int maxn = 1e6+10;
struct node{
int address;
int next;
char key;
}nod[maxn];
int head1,n;
vector<node> list1;
cin>>head1>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;//重点1
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){ //重点2
list1.push_back(nod[head1]);
}
学会模拟链表之后,PAT甲级的链表题就都能做了,万变不离其宗,
基本就是,建立链表、按照题意操作链表(链表合并、去重、反转...)、注意判断链表边界(链表有效长度)、输入输出( %05d 来输出地址)
2019秋季PAT 7-2
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100001;
int ad1,ad2,n;
int head1,head2 = 0;
int len1 = 0,len2 = 0;
struct node{
int add,next,key;
}nod[maxn];
vector<node> list1;
vector<node> list2;
vector<node> res;
int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++){
int add,next,key;
cin>>add>>key>>next;
nod[add].add = add;
nod[add].next = next;
nod[add].key = key;
}
for (int p = head1; p != -1; p = nod[p].next)
list1.push_back(nod[p]);
for(int p = head2;p!=-1;p = nod[p].next)
list2.push_back(nod[p]);
len1 = list1.size()-1;
len2 = list2.size()-1;
if(len1 >= 2*len2){
int p = 0,q = len2;
while(q >= 0){
res.push_back(list1[p++]);
res.push_back(list1[p++]);
res.push_back(list2[len2--]);
}
while(p <= len1){
res.push_back(list1[p++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}else{
int p = len1,q = 0;
while(p >= 0){
res.push_back(list2[q++]);
res.push_back(list2[q++]);
res.push_back(list1[p--]);
}
while(q<=len2){
res.push_back(list2[q++]);
}
for(int i=0;i<res.size();i++){
if(i != res.size() - 1){
res[i].next = res[i+1].add;
}
}
}
for(int i=0;i<res.size();i++){
if(i == res.size()-1) printf("%05d %d -1\n",res[i].add,res[i].key);
else printf("%05d %d %05d\n",res[i].add,res[i].key,res[i].next);
}
return 0;
}
PTA1032
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
struct node{
int address;
int next;
char key;
}nod[maxn];
int head1,head2,n;
bool use[maxn];
vector<node> list1,list2;
int main(){
cin>>head1>>head2>>n;
for(int i=1;i<=n;i++) {
int address,next;
char key;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
}
for(head1; head1 != -1; head1 = nod[head1].next){
list1.push_back(nod[head1]);
use[head1] = true;
}
for(head2; head2 != -1; head2 = nod[head2].next){
if(use[head2]){
printf("%05d\n",head2);
return 0;
}
}
cout<<"-1"<<endl;
return 0;
}
PTA1052
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;
struct node{
int address;
int key;
int next;
bool flag;
}nod[100010];
vector<node> list1;
int n;
int head;
bool cmp(node a,node b){
return a.key < b.key;
}
int main(){
cin>>n>>head;
for(int i=1;i<=n;i++){
int address,key,next;
cin>>address>>key>>next;
nod[address].address = address;
nod[address].key = key;
nod[address].next = next;
nod[address].flag = true;
}
int m = 0;
for(;head!=-1;head = nod[head].next){
nod[head].flag = true;
list1.push_back(nod[head]);
m++;
}
if(m == 0){ //判边界 否则段错误
printf("0 -1\n");
return 0;
}
sort(list1.begin(),list1.end(),cmp);
for(int i=0;i<m-1;i++){
list1[i].next = list1[i+1].address;
}
cout<<m<<" ";
printf("%05d\n",list1[0].address);
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i].next);
}
printf("%05d %d -1\n",list1[m-1].address,list1[m-1].key);
return 0;
}
PTA1074
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
/*
题目意思是 k个元素间换一下
*/
struct node{
int address;
int key;
int next;
}nod[maxn];
vector<node>list1,list2;
int head,n,k;
void print(){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i].next);
}
printf("%05d %d -1\n",list2[n-1].address,list2[n-1].key);
}
int main(){
cin>>head>>n>>k;
for(int i=0;i<n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
}
for(;head!=-1;head=nod[head].next)
list1.push_back(nod[head]);
n = list1.size(); //原来的n不一定是新链表的长度 可能输入数据有无效结点
int pos = 0;
while(pos + k - 1 < n){ //这里注意 下标 每pos~pos+k-1为一组
for(int i = pos+k-1;i>=pos;i--){ //倒序存进list2
list2.push_back(list1[i]);
}
pos+=k;
}
if(pos < n){ //剩下的没有凑够k个就 直接存入链表list2
for(int i=pos;i<n;i++){//正序存进list2
list2.push_back(list1[i]);
}
}
for(int i=0;i<n-1;i++){ //更新next地址
list2[i].next = list2[i+1].address;
}
list2[n-1].next = -1;
print();
return 0;
}
/*
00100 4 2
00000 4 -1
00100 1 12309
33218 3 00000
12309 2 33218
*/
PTA1097
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int head,n;
struct node{
int address;
int key;
int next;
}nod[maxn];
bool vis[maxn];
vector<node> list1,list2;
int main(){
cin>>head>>n;
for(int i = 0;i < n;i++){
int add,key,next;
cin>>add>>key>>next;
nod[add].address = add;
nod[add].key = key;
nod[add].next = next;
}
for(;head!=-1;head = nod[head].next){
if(!vis[abs(nod[head].key)]){
vis[abs(nod[head].key)] = true;
list1.push_back(nod[head]);
}else{
list2.push_back(nod[head]);
}
}
n = list1.size();
if(n != 0){
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i+1].address);
}
printf("%05d %d -1\n",list1[n-1].address,list1[n-1].key);
}
int m = list2.size();
if(m != 0){
for(int i=0;i<m-1;i++){
printf("%05d %d %05d\n",list2[i].address,list2[i].key,list2[i+1].address);
}
printf("%05d %d -1\n",list2[m-1].address,list2[m-1].key);
}
return 0;
}
PAT甲级专题|链表的更多相关文章
- PAT甲级专题|树的遍历
PAT甲级专题-树的遍历 涉及知识点:树.建树.深度优先搜索.广度优先搜索.递归 甲级PTA 1004 输出每一层的结点,邻接表vector建树后.用dfs.bfs都可以边搜边存当前层的数据, #in ...
- PAT甲级专题|最短路
PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- PAT甲级题分类汇编——理论
本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...
随机推荐
- Oracle Dorp 表数据恢复
利用Oracle 数据回闪机制进行恢复,当一个表被drop掉,表会被放入recyclebin回收站,可通过回收站做表的闪回.表上的索引.约束等同样会被恢复不支持sys/system用户表空间对象,可通 ...
- [考试反思]0916csp-s模拟测试44:可笑
出现了有史以来第一个3首杀AK啊...然而跟我没有丝毫关系 (曾经还是有一次考试差点就有那么一点关系的...) 然而反正我考得很水就是了.不是很垃圾,而是很水. 这套题是真的水... T1不会证复杂度 ...
- 爬虫--requests爬取猫眼电影排行榜
'''目标:使用requests分页爬取猫眼电影中榜单栏目中TOP100榜的所有电影信息,并将信息写入文件URL地址:http://maoyan.com/board/4 其中参数offset表示其实条 ...
- 2019年10月13日 spss习题 wangqingchao
1.spss发行版本的说法,正确的是:B a.两年发行一个新版本 b.一年发行一个新版本 c.没有任何规律 d.三年发行一个新版本 2.哪些是spss统计分析软件的基本窗口:A a.结果查看 ...
- pscp命令详解
注意:只能在winds下执行 环境准备 1.先下载pscp软件,这里是我的云盘地址: 链接:https://pan.baidu.com/s/1mkzRMv-aosC94KbMcMea9w 提取码:k0 ...
- 在VMware CentOS7挂载系统光盘搭建本地仓库
1.软件准备: 安装VMware环境,在这里我使用的是VMware15 一个虚拟机系统,在这里我使用的是CentOS7(版本不同可能会有一点出入,但是应该相差不大) 在这里还有一个前提是已经建立好了y ...
- CAS3.5.2 Server登录后返回用户信息详细解决方案
单点登录(Single Sign-On, 简称SSO)是目前比较流行的服务于企业业务整合的解决方案之一,SSO使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.大家在使用时CA ...
- 一个excel(20M)就能干趴你的poi,你信吗?
自从上一篇:一个普通类就能干趴你的springboot,你信吗?后,很巧的是这次又发现一个问题,所以有了这篇文章,还是想沿用上篇的”流水帐“的方式查找问题和解决问题.这篇文章主要是因为使用POI导入一 ...
- Windows下mysql的下载和安装
下载: 1.下载地址:https://www.mysql.com/downloads/ 2.选择社区版:MySQL Community (GPL) Downloads » 3.MySQL Commun ...
- [HTML] 学HTML写的第一第二个网页
①第一个网页 <h2>英雄联盟(电子竞技类游戏)</h2> <p><b>(英雄联盟)</b>(简称lol)是由美国<i>Roit ...