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

思路:

方法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. Django开发点菜系统学习笔记

    1.使用django-simple-captcha包的时候,会调用到: register_form = RegisterForm(request.POST) 但是这个时候captcha不进行错误检验, ...

  2. WPF 通过 CommandParameter 传递当前窗体到 ViewModel

    在应用 Command 模式中,需要在View上点击 一个按钮,需要将当前窗体作为参数传递为 command 两种方式传递当前窗体1.通过窗体名称(假设窗体名称为 ThisWindow)   < ...

  3. InnoDB存储引擎的B+树索引算法

    关于B+树数据结构 ①InnoDB存储引擎支持两种常见的索引. 一种是B+树,一种是哈希. B+树中的B代表的意思不是二叉(binary),而是平衡(balance),因为B+树最早是从平衡二叉树演化 ...

  4. 用I/O口模拟总线时序

    在做总线通信过程中,我们很少会用到这样方法,一般在我们选择MCU的时候都会带有你所需要的通信接口.但是,对于一些简单的通信应该用的场合,一般在一些传感器的数据通信过程中,传感器厂商会将通信协议做一些改 ...

  5. ubuntu下安装交叉编译工具链

    /usr/localmkdir arm 将文件file1复制成文件file2 cp file1 file2 /cp /mnt/hgfs/UbuntuGX/arm-2008q3-linux.tar.gz ...

  6. request.setAttribute("username", username);//一定要保存,OGNL才能获取${username}

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  7. 2017.11.7 Python 制作EFM32/ AVR批量烧录工具

    Customer need program quickly asap. ok,I need to set up a table for test. 1 reference data http://ww ...

  8. C#中upd分包与发送,已经实现全部代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Tool ...

  9. BAT级别对照表

  10. linux TCP Fast Open开启和测试

    linux上要开启TCP Fast Open,内核版本至少为3.7.0, 且需要设置 /proc/sys/net/ipv4/tcp_fastopen 为3. 开启后,如果有连接进来,使用如下命令查看: ...