题意:寻找两个链表的首个公共结点,输出其地址。

思路:

方法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的更多相关文章

  1. 【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 ...

  2. PAT 1032 Sharing[hash][链表][一般上]

    1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...

  3. 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 ...

  4. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  5. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  6. 1032. Sharing (25) -set运用

    题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...

  7. 1032. Sharing (25)

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  8. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. 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. ...

  10. PAT 1032. Sharing

    其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...

随机推荐

  1. 构造函数=default表示什么?

    myClass()=default;//表示默认存在构造函数

  2. mysql create database and user 新建数据库并为其创建专用账号

    DROP DATABASE `wordpress`;------------------------------------------------------------------ CREATE ...

  3. centOS7.4 thinkPHP nginx 支持pathinfo和rewrite

    server { listen 80; server_name www.demo.com mayifanx.com; root /data/www/demo; index index.php inde ...

  4. Java小程序之输出星号

    题目:打印出如下图案(菱形)     *    ***  ****** ********  ******   ***    * 编程工具使用eclipse 代码如下: package test; pu ...

  5. Shell for、while循环

    先顺带说下 if 1. if  条件;then else fi 如果else分支没有执行语句,可以不写. 2. if 条件;then elif 条件;then else fi #!/bin/bash ...

  6. matlab 学习笔记

    脚本名称不能与matlab里面的关键字一样.否则会报当MATLAB中报错,“SCRIPT ******”怎么解决 保留已画图形:hold on 矩阵连接:横向 f=[m,n];   纵向 f=[m;n ...

  7. spring: @RequestMapping注解

    处理GET/POST请求方法 1.常用的: import org.springframework.web.bind.annotation.RequestMapping; @Controller pub ...

  8. MyEclipse安装git插件

    安装egit插件的步骤(安装egit不成功的原因主要是下载的egit版本不适合当前使用的eclipse版本).先检查自己MyEclipse适用egit的版本. 查看自己MyEclipse版本,如下图: ...

  9. android开发环境:使用Android Studio搭建Android集成开发环境(图文教程)

    开发环境情况: 物理机版本:Win 7旗舰版(64位) Java SDK版本:jdk1.8.0_25(64位) Android SDK版本:Android 7.1(API 25) Android St ...

  10. 使用swagger作为restful api的doc文档生成——从源码中去提取restful URL接口描述文档

    初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...