1032 Sharing
题意:寻找两个链表的首个公共结点,输出其地址。
思路:
方法1. 如果LinkList1比LinkList2长,则让LinkList1先偏移(len1-len2)个结点,然后,让两个链表的的工作指针同时开始偏移,一旦遇到结点地址相同且数据域相同则退出。
方法2(更简洁). 首先遍历LinkList1,每访问一个结点,标记该结点为已经访问;然后,遍历LinkList2,一旦遇到当前结点已经被访问过了,就退出。
代码:
(方法1)
#include <cstdio>
;
struct Node{
char data;
int next;
}LinkList[N];
int myStrlen(int head)
{
;
){
len++;
head=LinkList[head].next;
}
return len;
}
int main()
{
//freopen("pat.txt","r",stdin);
int head1,head2,n;
scanf("%d%d%d",&head1,&head2,&n);
int curr,next;
char data;
;i<n;i++){
scanf("%d %c %d",&curr,&data,&next);
LinkList[curr].data=data;
LinkList[curr].next=next;
}
int len1=myStrlen(head1);
int len2=myStrlen(head2);
int p1=head1,p2=head2;
if(len1>len2){
int cnt=len1-len2;
while(cnt--){
p1=LinkList[p1].next;
}
}else if(len1<len2){
int cnt=len2-len1;
while(cnt--){
p2=LinkList[p2].next;
}
}
&& p2!=- ){
if(LinkList[p1].data==LinkList[p2].data && p1==p2) break;
p1=LinkList[p1].next;
p2=LinkList[p2].next;
}
) printf("%d",p1);
else printf("%05d",p1);
;
}
(方法2)
#include <cstdio>
;
struct Node{
char data;
int next;
bool vis;
}LinkList[N];
int main()
{
//freopen("pat.txt","r",stdin);
int head1,head2,n;
scanf("%d%d%d",&head1,&head2,&n);
int curr,next;
char data;
;i<n;i++){
scanf("%d %c %d",&curr,&data,&next);
LinkList[curr].data=data;
LinkList[curr].next=next;
LinkList[curr].vis=false;
}
int p=head1;
){
LinkList[p].vis=true;
p=LinkList[p].next;
}
p=head2;
bool flag=false;
){
if(LinkList[p].vis){
flag=true;
break;
}
p=LinkList[p].next;
}
if(flag) printf("%05d",p);
else printf("-1");
;
}
1032 Sharing的更多相关文章
- 【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[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) 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中查 ...
- 1032. Sharing (25) -set运用
题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...
- 1032. Sharing (25)
To store English words, one method is to use linked lists and store a word letter by letter. To save ...
- PAT甲题题解-1032. Sharing (25)-链表水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 1032 Sharing (25)(25 point(s))
problem To store English words, one method is to use linked lists and store a word letter by letter. ...
- PAT 1032. Sharing
其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...
随机推荐
- JNIjw05
ZC: 这个代码,没有真正的运行测试 1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw05.h" #in ...
- virtio,vhost 和vhost-user
随着qemu2.1的发布,可以看到,qemu支持了vhost-user.从介绍可以看出,这是把原来vhost-backend从kernel移到了userspace,这和原来virtio架构有什么区别呢 ...
- python学习笔记(virtualenv下载安装)
之前博客评论中有人建议我面对多个python版本的情况.可以使用virtualenv这个python虚拟沙盒 首页是利用pip下载.关于pip如何下载安装前面的博客中已经提到就不细说 cmd直接进入p ...
- Python 用Redis简单实现分布式爬虫
Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台. 连接数据库 注意:假设现有几台在同一局域网内的机器分别为Master和几个Slaver Master连接 ...
- 【Python】unicode' object is not callable
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.
- Struts12---文件的下载
01.创建一个下载的页面 (我们的下载是把文件的路径写成固定的) <body> <form action="user/download" method=&quo ...
- LeetCode OJ:Perfect Squares(完美平方)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- mcake活动维护,检查 ★ ★
一.检查火狐浏览器.chrome浏览器.ie8.9.10.11是否显示正常 二.对比设计稿 三.动画添加 四.检查图片是否失真 五.ie8空按钮无法点击 六.官网banner制作
- ubuntu1604-server上安装virtualbox+phpvirtualbox
1.需要安装phpvirtualbox版本与virtualbox的版本一致,比如phpvirtual5.0.x,需要对应virtualbox 5.0.x 2.需要安装的软件有apache2.php.l ...
- TF随笔-4
>>> import tensorflow as tf>>> a=tf.constant([[1,2],[3,4]])>>> b=tf.const ...