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 ...
随机推荐
- "Entity Framework数据插入性能追踪"读后总结
园友莱布尼茨写了一篇<Entity Framework数据插入性能追踪>的文章,我感觉不错,至少他提出了问题,写了出来,引起了大家的讨论,这就是一个氛围.读完文章+评论,于是我自己也写了个 ...
- D3.js学习笔记(四)—— 使用SVG坐标空间
目标 在这一章,你将要使用D3.js基于一些数据把SVG元素添加到你想要的坐标位置上. 我们的目标就是使用下面的数据集: var spaceCircles = [30,70,110]; 并使用D3.j ...
- Redis分区探究
Redis比较好的分区算法是采用Hash分区算法 也就是我们可以将所有的server例如:user1,user2,user3.通过hash函数将key转化为一个数字然后求余找到需要存储的server. ...
- HYSBZ - 2005 莫比乌斯反演
链接 对于gcd(i,j)的位置来说,对答案的贡献是2*(gcd(i,j)-1)+1,所以答案ans ans=Σ(1<=i<=n)(1<=j<=m)2*(gcd(i,j)-1) ...
- hdu 5981 Guess the number
Guess the number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 160000/160000 K (Java/Other ...
- ASM9260T开发板使用
ifconfig eth0 192.168.1.66 netmask 255.255.255.0 up //设备iproute add default gw 192.168.1.1 //添加网 ...
- Foundations of Qt Development 学习笔记 Part1 Tips1-50
1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...
- ip分包研究-以UDP为例
原文 http://www.jianshu.com/p/741cb12ab0c9 测试环境: 利用iOS的NE从TUN抓取IP packets,如下代码分析ip包: uint16_t iphid = ...
- Kivy: Building GUI and Mobile apps with Python
Intro Python library for building gui apps (think qt, gdk,processing) build from ground up for lates ...
- asp.net identity的学习记录
# identity数据库 ## 创建空数据库 交给ef管理 ### 添加asp.net identity包 ``` Install-Package Microsoft.AspNet.Identity ...