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 ...
随机推荐
- pandas 数据处理
1. 查看数值数据的整体分布情况 datafram.describe() 输出: agecount 1463.000000mean 22.948052std 8.385384min 13.000000 ...
- yii2: 点击编辑后,左侧的连接(a.navtab)失效,变成在新窗口打开
如:图一 使用a.navtab的时候,点击[自定义回复]->右侧列表,随便编辑一个,完成后 图二: 再点击,左侧的菜单,打开iframe就会失败,直接在新窗口打开.源代码如下: 造成这样的原因是 ...
- 【C#笔札】1 string类型
C中没有string这个类型,而是用字符串数组来实现,相对来说比较麻烦. LABVIEW相对来说要简单太多,毕竟他主要的精力集中在硬件控制上,软件操作方面极其简单. C#类似,C#中有System.S ...
- mysql 分区优缺点
分区不一定能够提高查询速度: 原因:分区是按字段分区,如果查询条件不是分区字段会很慢.
- idea解决@Data注解报红问题
lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具,简单来说,比如我们新建了一个类,然后在其中写了几个字段,然后通常情况下我们需要手动去建立ge ...
- 0 与 “0" 与 '\0' 与 '0'相互之间的区别
1. '\0'和‘0’都是字符,对应的ASCII值分别是0和48. 2. 0表示一个数字.也可以表示ASCII值,对应字符'\0'. 3. “0”表示字符串,第一个字符是'0'.
- 【scala】集合框架
- SQLSERVER store procedure 临时表
有些时候显示重复数据时,使用: ) 但有些时候表A过大或者逻辑复杂.显示数据时,会造成性能的影响,这时你就可以使用临时表: ) create table #temp( XXX , XXX) )in ...
- idea远程debug调试设置
1.idea设置 1.1 加入Tomcat Server选择Remote 1.2:设置对应的參数 xxx.xxx.152.67:8080为远程Tomcatserver的IP地址和port,这里能够设置 ...
- 一步一步写jQuery插件
转载自:http://www.cnblogs.com/joey0210/p/3408349.html 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquer ...