PAT 1032. Sharing
其实就是链表求交:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unordered_map> using namespace std; class Node {
public:
Node() : data(), next() {}
char data;
int next;
}; void print_list(unordered_map<int, Node*>& mem, int head) {
int cur = head;
while (cur != -) {
cout<<mem[cur]->data<<" ";
cur = mem[cur]->next;
}
cout<<endl;
}; int step_list(unordered_map<int, Node*>& mem, int from, int n) {
if (n < ) return -;
int cur = from;
while (cur != -) {
if (n == ) {
break;
}
cur = mem[cur]->next;
n--;
}
return cur;
} int count_list(unordered_map<int, Node*>& mem, int from) {
if (from < ) return ;
int cur = from;
int cnt = ;
while (cur != -) {
cur = mem[cur]->next;
cnt++;
}
return cnt;
} int main() {
int head_a, head_b;
int n; unordered_map<int, Node*> mem; cin>>head_a>>head_b>>n; for (int i=; i<n; i++) {
int addr;
Node* np = new Node();
scanf("%d %c %d", &addr, &(np->data), &(np->next));
mem.insert(make_pair(addr, np));
} int cnt_a = count_list(mem, head_a);
int cnt_b = count_list(mem, head_b); int cnt_diff = cnt_a - cnt_b;
int head_long = head_a;
int head_short = head_b;
if (cnt_diff < ) {
cnt_diff = -cnt_diff;
head_long = head_b;
head_short= head_a;
} head_long = step_list(mem, head_long, cnt_diff); while (head_long != head_short) {
head_long = mem[head_long]->next;
head_short= mem[head_short]->next;
}
if (head_long < ) {
printf("-1\n");
} else {
printf("%05d\n", head_long);
}
system("pause");
return ;
}
PAT 1032. Sharing的更多相关文章
- PAT 1032 Sharing[hash][链表][一般上]
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- 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】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 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- 1032 Sharing (25分)
1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...
- PAT甲题题解-1032. Sharing (25)-链表水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT 甲级 1032 Sharing
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...
- PAT (Advanced Level) 1032. Sharing (25)
简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...
随机推荐
- 启动express,端口被占用
启动express项目报错: root@ubuntuServerVM:/home/nodejs/meadowlark/site# node meadowlark.jsevents.js:160 thr ...
- 【python】10分钟教你用python打造贪吃蛇超详细教程
10分钟教你用python打造贪吃蛇超详细教程 在家闲着没妹子约, 刚好最近又学了一下python,听说pygame挺好玩的.今天就在家研究一下, 弄了个贪吃蛇出来.希望大家喜欢. 先看程序效果: 0 ...
- P1273 有线电视网(树形dp)
P1273 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. ...
- 为阿里云域名配置免费SSL支持https加密访问简单教程
阿里云之前有免费ssl入口申请,现在已经关闭了.那么现在怎么为自己的域名配置https呢? 首先打开阿里云域名控制台,如以下界面.(这里暂且用我的这个域名讲解吧) 如上图点击ssl证书,点击单域名免 ...
- Java多线程——对象组合
我们不希望对每一次的内存访问都进行分析以确保程序是线程安全的,而是希望将一些现有的线程安全组件组合为更大规模的组件或者程序,这里介绍一些组合模式,这些组合模式能够使一个类更容易成为线程安全的,并且在维 ...
- QuantLib 金融计算——QuantLib 入门
目录 QuantLib 金融计算--QuantLib 入门 简介 主要功能 安装与使用 学习指南 The HARD Way The EASY Way QuantLib 金融计算--QuantLib 入 ...
- 实验一 c++简单程序设计
一.实验内容 1.ex 2_28 (1) 用if...else判断 #include<iostream> using namespace std; int main() { char i; ...
- java中的POJO、PO、VO分别是什么?
1.PO:persistant object 持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是不错的选择. 2. VO:value object值对象. 通常 ...
- WPF 自定义ScrollViwer
ScrollViewer自定义样式 ScrollViewer在各种列表.集合控件中广泛使用的基础组建,先看看效果图: 如上图,ScrollViewer简单来说分两部分,一个横向的滚动条,一个垂直滚动条 ...
- linux安装QQ截图
本人(壮壮熊)现用系统是ubuntu 12.04 相信用过linux系统的朋友都知道,linux下的截图软件是在不咋的.虽然系统本身有带截图工具,但是却苦于没有办法在截下来的图片上作画圈.写文字说明等 ...