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 ...
随机推荐
- JSP报错01
ZC: 创建一个JSP之后,就报如下错误. 1.错误: 1.1. 2.处理: Exlipse for JEE --> Project Explorer 中选择项目 --> 右击项目名 -- ...
- scons的使用
以下测试是在linux下. 1.安装. $sudo apt install scons 2.查看安装版本: $scons --version 会出现以下内容: SCons by Steven Knig ...
- 关于VirtualBox在桥接模式下无法联网解决方案
关于VirtualBox在桥接模式下无法联网 解决方案VirtualBox与笔记本无线网卡桥接 如果重新开机连接不上,可能需要重新共享一下,(重新给桥接网卡分配与当前物理机IP在同一局域网段的IP) ...
- C# 捕获数据库自定义异常
在 SQL Server 的存储过程中根据业务逻辑的要求,有时需要抛出自定义异常,由C#程序俘获之并进行相应的处理.SQL Server 抛出自定义异常和简单,像这样就可以了:RAISERROR('R ...
- angular指令(二)--内置指令
一.基础ng 属性指令: ng-href ng-src ng-disabled ng-checked ng-readonly ng-selected ng-class ng-styl ...
- IOS-视频
一.简介 iOS提供了MPMoviePlayerController.MPMoviePlayerViewController两个类,可以用来轻松播放视频和网络流媒体\网络音频 提示:网络音频同样使用此 ...
- [转载]java向word模板中填充数据(总结)
使用过PageOffice动态生成word文档的人都知道,PageOffice可以给word文档的指定位置进行填充,这里我们所说的指定位置在PageOffice的专业术语里面有两个概念,一个叫做数据区 ...
- Node.js/Python爬取网上漫画
某个周日晚上偶然发现了<火星异种>这部漫画,便在网上在线看了起来.在看的过程中图片加载很慢,而且有时候还不小心点到广告,大大延缓了我看的进度.后来想到能不能把先把漫画全部抓取到本地再去看. ...
- List排序共通代码
此共通方法可以根据特定字段进行排序 package com.gomecar.index.common.utils; import java.lang.reflect.Method; import ja ...
- Selenium2+Python自动化学习笔记(第1天)
参考[http://blog.csdn.net/henni_719/article/details/51096531]大神写的笔记,多谢大神共享. 哈哈,今天又找到一位大神写的Selenium2+Py ...