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 ...
随机推荐
- PHP服务器变量$_SERVER
常用的就几个,自己print_r($_SERVER); 看看. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv ...
- Java泛型中的标记符含义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...
- 20165332实验三 敏捷开发与XP实践
20165332实验三 敏捷开发与XP实践 实验内容 1:XP基础 2:XP核心实践 3:相关工具 实验1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化, ...
- python脚本1_给一个半径求圆的面积和周长
#给一个半径,求圆的面积和周长,圆周率3.14 r = int(input('r=')) print('area='+str(3.14*r*r)) print('circumference='+str ...
- IIS注册.NET
IIS中ASP.NET的版本号此时可选的有1.1.2.0和4.0三个,如果想让IIS把3个版本都集成上,那NET Framework 3种都要安装,默认安装到的是C 盘. IIS注册方式如下:1.1: ...
- Map类集合
集合类 Key Value ...
- kvm学习篇
云计算:一种资源的使用模式 弹性,按需付费资源降低成本 公有云:亚马逊.阿里云私有云:混合云: 安装: yum install qemu-kvm qemu-kvm-tools virt-manager ...
- 原子变量与CAS算法
原子变量 为了引出原子变量这个概念,我们先看一个例子. package com.ccfdod.juc; public class TestAtomicDemo { public static void ...
- Java加载jar文件并调用jar文件当中有参数和返回值的方法
在工作当中经常遇到反编译后的jar文件,并要传入参数了解其中的某些方法的输出,想到Java里面的反射可以实现加载jar文件并调用其中的方法来达到自己的目的.就写了个Demo代码. 以下的类可以编译生成 ...
- what is out of band mode.
Most of the steps are the same, except instead of sending an URL as the oauth_callback to request_to ...