其实就是链表求交:

 #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的更多相关文章

  1. PAT 1032 Sharing[hash][链表][一般上]

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  2. PAT 1032 Sharing (25分) 从自信到自闭

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

  3. 【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 ...

  4. 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 ...

  5. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  6. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  7. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  8. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  9. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

随机推荐

  1. YC全球总裁:我招揽陆奇好多年,如今终于如愿了!

    简评:YC 老大多年来一直在努力招揽陆奇,终于如愿了. YC 总裁 Sam Altman 在 8 月 14 日,发布了关于陆奇以及 YC 中国的公告,全文如下: 我非常荣幸地宣布,陆奇加入 YC 并且 ...

  2. 浅析Postgres中的并发控制(Concurrency Control)与事务特性(下)

    上文我们讨论了PostgreSQL的MVCC相关的基础知识以及实现机制.关于PostgreSQL中的MVCC,我们只讲了元组可见性的问题,还剩下两个问题没讲.一个是"Lost Update& ...

  3. js及Java中对于两个时间日期的判断脚本

    JS脚本: function checkDateIsEdited(createDate) { var compareDate = createDate.replace("-",&q ...

  4. leetcode-74-搜索二维矩阵

    题目描述:  编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: ...

  5. 响应式Web设计-一种优雅的掌上展现

    入门 flat - style (too many ad.) writeshell

  6. 参数化测试与Mock

    参数化测试与Mock 转载自https://blog.csdn.net/sunliduan/article/details/42026509 单元测试概念 说到测试,大家都不会陌生,从我们开始学习编程 ...

  7. Universal-Image-Loader完全解析(下)

    Universal-Image-Loader完全解析(下) 在这篇文章中,我会继续跟大家分享有关于Universal-Image-Loader框架的相关知识,这次主要分享的是框架中图片缓存方面的知识. ...

  8. 没事用html5 canvas画一个仪表盘自用,自适应的哦

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. PHP 对字符串 进行填充

    1.str_pad — 使用另一个字符串填充字符串为指定长度 . 我觉得str_pad可以满足普通的数字,字符串的简单填充. string str_pad ( string $input , int ...

  10. JavaScript设计模式-21.命令模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...