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 ...
随机推荐
- SpringData JPA实现CRUD,分页与多参数排序
Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等,SpringData JPA是简化创建 JPA ...
- Unicode字符串索引
一.目标 在通讯录中,我们有很多联系人,需要把这些联系人进行索引.对于每一个索引项对应的若干字符串,需要对这些字符串进行排序. 需要解决两个问题: 如何确定某个汉字应该被哪个字符索引? 某个索引项对应 ...
- sqlplus登录远程数据库与数据导出
一.登录 1.cmd中输入sqlplus /nolog 2.链接数据库,root是用户名,root123是密码,ORCL是数据库名.conn root/root123@192.168.1.27:152 ...
- 关于nginx的一个错误操作记录
今天在弄前后端同步的测试的时候,前端用Nginx代理访问后端接口,由于启动了多次nginx软件,没有将前几次启动的nginx进程关闭,导致在访问后端接口的request被挂起,过了半天也没有结果返回, ...
- 遇见Navicat 2003-can't connect to MYSQL server on 'localhost'(10061)
学习过程中难免遇到问题,今天就遇到了Navicat 2003-can't connect to MYSQL server on 'localhost'(10061),navicat报错,我就纳闷以前都 ...
- win10 压缩包安装mysql8.0.11报错:Access denied for user 'root'@'localhost'
按这篇:https://blog.csdn.net/Myuhua/article/details/84792121#commentsedit 这里精简下,还有update语句中authenticati ...
- Sql语句里的递归查询 SqlServer2005和Oracle 两个版本
以前使用Oracle,觉得它的递归查询很好用,就研究了一下SqlServer,发现它也支持在Sql里递归查询举例说明:SqlServer2005版本的Sql如下:比如一个表,有id和pId字段,id是 ...
- MySQL中show语法
1. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称. 2. show databases; -- 显示mysql中所有数据 ...
- 19.Class的基本语法
1.简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数. function Point(x, y) { this.x = x; this.y = y; } Point.proto ...
- 在Linux上创建webrev[基于git]
在Sun/Oracle工作了N(>12)年后,对webrev工具甚为喜欢,因为其易用性确实非常好.幸运的是,有工程师将webrev工具放到了GitHub上,而且支持git. 下面给出使用webr ...