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

思路:

方法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. TCP_AIO_Server_ZC_02

    ZC: 这个例子是,1个skt 投递 N个未决的接收操作 (记得 以前查过 说 线程数是 CUP数量的2倍 比较合适) 1. // 当需要 投递多个接收操作的时候,可以将接收缓冲封装成类,然后再投递多 ...

  2. ps切图步骤

    1.复制图层到新建 2.alt + i + r  裁剪 依次按 3.ctrl + alt + shift + s  保存 裁剪图标  复制到图层 , 删除背景,并复制样式 就可以做到 背景透明.

  3. es6 中的let,const

    在es6中,let的作用和var差不多,都是用来声明变量的,但是他们之间的区别在于作用域不同,大家都知道在js中没有块级作用域,例如: for(var i=0;i<10;i++){ consol ...

  4. android6.0、7.0、8.0新特性总结之开发应用时加以考虑的一些主要变更。

    android6.0 参考一:简书Android 6.0 新特性详解 参考二:关于Android6.0以上系统的权限问题 参考三:值得你关注的Android6.0上的重要变化(一) 参考四:值得你关注 ...

  5. 教你10步闯进google play排行榜前列

        1.正视最高榜单的价值 我们需要了解排名对你的游戏的价值,进入前20名你的游戏获得每日至少1万5千的安装量,而前10名获得至少2万5千的安装量.通过奖励性广告网络而获得这些流量需要你每日支付至 ...

  6. Node.js权威指南学习记录

    学习nodeJS权威指南的学习记录 导航: 1.console模块 2.全局变量 3.Buffer对象 4.事件对象 5.网络请求 6.文件操作对象 一. COMMON.js的学习.(commonJS ...

  7. 【scala】模式匹配

    Scala的模式匹配是通过match表达式从若干可选项中选择,类似Java中的switch. 例子: val firstArg = if(args.length>0) args(0) else ...

  8. opencv:图像的腐蚀和膨胀

    1.图像的腐蚀 图像的腐蚀和膨胀都是相对于像素值高(白色方向)说的,腐蚀简单的说就是白色”被腐蚀“了,也就是像素值低(黑色方向)的变多,白色变少. 腐蚀的原理是利用一个内核对图像进行卷积(扫描),内核 ...

  9. 团队作业:第五周 Alpha版本测试与发布

    团队:你吼辣么大声干什么嘛 Alpha版本测试报告:    bug: 修复的bug:     不能重现的bug:  无 产品设计,非bug: 在双人对战模式中,撞到墙壁会从对面的墙壁穿出,不会死 没能 ...

  10. 模拟form提交数据

    最近在做一个项目,发现ajax不能enctype=”multipart/form-data” 属性的表单,没办法,只能使用form表单直接提交的方法了,但是form表单直接提交会跳转页面,这样很不友好 ...