其实就是链表求交:

 #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. [Swift]遍历字符串

    Swift中无法再使用传统形式的for循环. //传统for循环形式不适用于Swift for(单次表达式;条件表达式;末尾循环体){中间循环体:} 字符串遍历方法1:使用该indices属性可以访问 ...

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

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

  3. Ubuntu16.04安装视觉SLAM环境(DBow3)

    1.从Github上现在DBow3词袋模型库 git clone https://github.com/rmsalinas/DBow3.git 2.开始安装DBow3库,进入DBow3目录 mkdir ...

  4. 服务器反爬虫攻略:Apache/Nginx/PHP禁止某些User Agent抓取网站

    我们都知道网络上的爬虫非常多,有对网站收录有益的,比如百度蜘蛛(Baiduspider),也有不但不遵守robots 规则对服务器造成压力,还不能为网站带来流量的无用爬虫,比如宜搜蜘蛛(YisouSp ...

  5. Numpy:np.vstack()&np.hstack() flat/flatten

    一 .  np.vstack: 按垂直方向(行顺序)堆叠数组构成一个新的数组 In[3]: import numpy as np In[4]: a = np.array([[1,2,3]]) a.sh ...

  6. mongoDb数据库可视化工具 --- Robo

    工具下载: https://robomongo.org/download 最终效果: 即我们可以直接看到链接到 27017端口 的数据的所有数据,并且可以对其进行行之有效的增删查改. 注意,在下载软件 ...

  7. Java调用C函数

    一.关于JNI JNI( Java Native Interface )主要是实现Java和C/C++语言之间的通信. Java通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOW ...

  8. python实现RSA加密解密方法

    python3.5 安装pip 安装rsa python -m pip install rsa 我们可以生成RSA公钥和密钥,也可以load一个.pem文件进来 # -*- coding: utf-8 ...

  9. Eclipse 安装spring插件spring tool suite(STS)

    安装方法有2种,一种是在线安装,比较方便,但耗时较长,一种是离线安装,步骤复杂一些,但耗时少,下面请看详细步骤. 方法一:在线安装     1:打开eclipse菜单Help>Eclise Ma ...

  10. hadoop-2.6.0-cdh5.4.5.tar.gz(CDH)的3节点集群搭建(含zookeeper集群安装)

    前言 本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接 http://www.cnblogs.com/zlslch/p/ ...