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 ...
随机推荐
- GeometryServer
http://blog.csdn.net/limina/article/details/8364515
- 分享知识-快乐自己:都说新的Arraylist 扩容是(1.5倍+1) 看了1.8的源代码发现不是这么回事
都说新的Arraylist 扩容是(1.5倍+1) 看了1.8的源代码发现不是这么回事 就用下面这段代码在jdk的三个版本运行看了下效果: import java.lang.reflect.Field ...
- hdu 5975 Aninteresting game
Aninteresting game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- react 文章
1.http://www.ruanyifeng.com/blog/2016/09/react-technology-stack.html (React 技术栈系列教程) 2.http://www.ru ...
- iptables(四)iptables匹配条件总结之一
经过前文的总结,我们已经能够熟练的管理规则了,但是我们使用过的"匹配条件"少得可怜,之前的示例中,我们只使用过一种匹配条件,就是将"源地址"作为匹配条件. 那么 ...
- webpack打包图片资源找不到问题
当我们进行前端打包时,需改成如下配置: 往常这样打包是没有问题的,可是今天进行项目打包的时候缺报图片找不到的错误,如图所示: 头部组件的图片资源找不到错误,后台发现因为头部组件的背景图片size过大, ...
- 使用VS自带的工具分析.NET程序的性能
(转自:http://www.cnblogs.com/DebugLZQ/archive/2012/07/10/2585245.html) 这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我 ...
- 【.Net】Socket小示例
引言 项目中用到了Socket,这里做个控制台小示例记录一下. Client 客户端的Receive用了异步方法,保持长连接,可以随时发送消息和响应服务端的消息,如下 static string Cl ...
- 软工作业-四则运算(java实现)BY叶湖倩,叶钰羽
四则运算生成器 BY-信安1班 叶湖倩(3216005170) 信安1班 叶钰羽(3216005171) 1. 项目介绍 源代码GitHub地址:https://github.com/yeyuyu/s ...
- 阿里maven镜像服务器配置
把下面的配置复制到 .m2/settings.xml配置文件中. <?xml version="1.0" encoding="UTF-8"?> &l ...