其实就是链表求交:

 #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. null、 is_null() 、empty() 、isset() PHP 判断变量是否为空

    PHP中,在判断变量是否为空的时候,总会纠结应该选用哪个函数,下面列取常用的多种情况,其中1/3经过我的验证,其它来自网络,验证后使用... 使用 PHP 函数对变量 $x 进行比较 表达式 gett ...

  2. mybaits中"#"和"$"的区别

    动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql ...

  3. C语言奇淫技巧,字符串的三种表示方法,不会用不是合格的程序员

    1.在C语言中,是将字符串作为字符数组来处理的,字符串是逐个存放到数组元素中的 例如用一个一维的字符数组存放字符串"I am a boy.",如下代码: char c[12] = ...

  4. Vue 父子组件传递方式

    问题: parent.vue <template> <div> 父组件 <child :childObject="asyncObject">&l ...

  5. vim中常用的命令

    1.光标的命令 gg 移到第一行位置 G 移到最后一行 o       行首 $       行末 nk 向上移动n行 nj 向下移动n行 nh 向左移动n列 nl 向右移动n列 ctrl+f     ...

  6. shell-005:备份。

    #  这个案例感觉有点绕,如果是本地机器备份完全没必要.下面带颜色的写法值得我们借鉴,所以还是做下笔记吧 #!/bin/bash # 找出//目录下所有txt结尾的文件,且形成一个列表清单的文件 fi ...

  7. CentOS7 安装 Visual Code

    官网下载 rpm 安装文件 运行 rpm -ivh 试一下 sudo rpm -ivh xxx.rpm 有可能弹出提出:libxx.so() need by xxxrpm ,反正意思是缺组件,去下载安 ...

  8. 什么是WCF(转)

    什么是WCF(Windows Communication Foundation(WCF)) 大家可以百度一下了解什么是WCF.当然有些人看到密密麻麻的黑框白字就懒的读.即使读了 可能也没明白确切的含义 ...

  9. ubuntu设置root权限默认密码

    1.默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd,然后输入当前用户的密码2.终端会提示我们输入新的密码并确认,此时的密码就是root新密 ...

  10. c++ 网络编程课设入门超详细教程 ---目录

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9663167.html c++ 网络编程(一)TCP/UDP windows/linux 下入门 ...