zoukankan      html  css  js  c++  java
  • pat1032. Sharing (25)

    1032. Sharing (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are stored as showed in Figure 1.


    Figure 1

    You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (<= 105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.

    Then N lines follow, each describes a node in the format:

    Address Data Next

    where Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and Next is the position of the next node.

    Output Specification:

    For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output "-1" instead.

    Sample Input 1:
    11111 22222 9
    67890 i 00002
    00010 a 12345
    00003 g -1
    12345 D 67890
    00002 n 00003
    22222 B 23456
    11111 L 00001
    23456 e 67890
    00001 o 00010
    
    Sample Output 1:
    67890
    
    Sample Input 2:
    00001 00002 4
    00001 a 10001
    10001 s -1
    00002 a 10002
    10002 t -1
    
    Sample Output 2:
    -1
    

    提交代码

    1.输出注意格式!!

    2.不要投机!!

    方法一:

    首先从第一个链表头开始遍历链表1, 对每个节点进行标记。然后从第二个链表头开始遍历链表2, 当碰到一个节点是已经被标记了的, 那么这个节点就是第一个公共节点。

     1 #include<cstdio>
     2 #include<stack>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 int mem[100005];
    10 bool vis[100005];
    11 int main(){
    12     //freopen("D:\INPUT.txt","r",stdin);
    13     int fa,fb,n;
    14     scanf("%d %d %d",&fa,&fb,&n);
    15     int i,point;
    16     char c;
    17     for(i=0;i<n;i++){
    18         scanf("%d",&point);
    19         cin>>c;
    20         scanf("%d",&mem[point]);
    21     }
    22     int pa=fa,pb=fb;
    23     while(pa!=-1){
    24         vis[pa]=true;
    25         pa=mem[pa];
    26     }
    27     while(pb!=-1){
    28         if(vis[pb]){
    29             break;
    30         }
    31         pb=mem[pb];
    32     }
    33     if(pb!=-1){
    34         printf("%05d
    ",pb);
    35     }
    36     else{
    37         printf("-1
    ");
    38     }
    39     return 0;
    40 }

    方法二:模拟链表法

     1 #include<cstdio>
     2 #include<stack>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 int mem[100005];
    10 int main(){
    11     //freopen("D:\INPUT.txt","r",stdin);
    12     int fa,anum=0,fb,bnum=0,n;
    13     scanf("%d %d %d",&fa,&fb,&n);
    14     int i,point;
    15     char c;
    16     for(i=0;i<n;i++){
    17         scanf("%d",&point);
    18         cin>>c;
    19         scanf("%d",&mem[point]);
    20     }
    21     int pa=fa,pb=fb;
    22     while(pa!=-1){
    23         anum++;
    24         pa=mem[pa];
    25     }
    26     while(pb!=-1){
    27         bnum++;
    28         pb=mem[pb];
    29     }
    30     pa=fa,pb=fb;
    31     while(anum>bnum){
    32         anum--;
    33         pa=mem[pa];
    34     }
    35     while(anum<bnum){
    36         bnum--;
    37         pb=mem[pb];
    38     }
    39     while(pa!=pb){
    40         pa=mem[pa];
    41         pb=mem[pb];
    42     }
    43     if(pa!=-1){
    44         printf("%05d
    ",pa);
    45     }
    46     else{
    47         printf("-1
    ");
    48     }
    49     return 0;
    50 }

    方法三:

    统计被指向节点,如果一个节点被指向2次,那么这个点就是公共开始点,或者-1出现2次。但由于测试样例的设计恰好避免了这种投机取巧。。。。此方法不能拿全分!!

     1 #include<cstdio>
     2 #include<stack>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 set<int> ha;
    10 int main(){
    11     //freopen("D:\INPUT.txt","r",stdin);
    12     int fa,fb,n,fin;
    13     scanf("%d %d %d",&fa,&fb,&n);
    14     int i,point;
    15     char c;
    16     for(i=0;i<n;i++){
    17         scanf("%d",&point);
    18         cin>>c;
    19         scanf("%d",&point);
    20         if(ha.count(point)==1){//第二次找到point
    21             fin=point;
    22         }
    23         ha.insert(point);
    24     }
    25     if(fin!=-1){
    26         printf("%05d
    ",fin);
    27     }
    28     else{
    29         printf("-1
    ");
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    02树莓派刷入系统
    Debian stretch更换国内源
    C#-WebForm-点击网页中的按钮后跳转到其他页面是怎么实现的?
    C#-WebForm-WebForm开发基础、如何给控件注册事件?——事件委托写法、http无状态性、三层结构
    C#-WebForm-表单元素
    C#-WebForm-ASP开发练习:从数据库中动态添加信息
    C#-WebForm-WebForm开发基础
    C#-WinForm-TextBox中只能输入数字的几种常用方法(C#)
    C#-和时间有关的计算代码、时间相减 得到天数、小时、分钟、秒差
    C#-WinForm-用户控件如何获取父级窗体
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4777324.html
Copyright © 2011-2022 走看看