PAT (Advanced Level) 1032. Sharing (25)

简单题,不过数据中好像存在有环的链表......

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
using namespace std;

const int maxn=100000+10;
struct Node
{
    int id;
    char c;
    int nx;
} node[maxn];
int add1,add2,n;

int s1[maxn],s2[maxn];
int len1=0,len2=0;
int m[maxn];

int main()
{
    memset(m,-1,sizeof m);
    scanf("%d%d%d",&add1,&add2,&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&node[i].id);
        m[node[i].id]=i;
        cin>>node[i].c;
        scanf("%d",&node[i].nx);
    }

    for(int i=1; i<=n; i++)
    {
        if(node[i].nx==-1) continue;
        node[i].nx=m[node[i].nx];
    }

    memset(s1,-1,sizeof s1);
    memset(s2,-1,sizeof s2);

    int now=m[add1];

    while(1)
    {
        if(now==-1) break;
        s1[len1++]=now;
        now=node[now].nx;
    }

    now=m[add2];

    bool flag[maxn];
    memset(flag,0,sizeof flag);

    int fail=0;

    while(1)
    {
        if(now==-1) break;
        if(flag[now]==1)
        {
            fail=1;
            break;
        }
        flag[now]=1;
        s2[len2++]=now;
        now=node[now].nx;
    }

    if(fail==1)
    {
        printf("-1\n");
    }
    else
    {
        len1--;
        len2--;
        while(len1>=0&&len2>=0)
        {
            if(s1[len1]==s2[len2])
            {
                len1--;
                len2--;
            }
            else break;
        }
        len1++;
        len2++;

        if(s1[len1]==-1) printf("-1\n");
        else printf("%05d\n",node[s1[len1]].id);
    }
    return 0;
}
时间: 2024-08-07 02:03:23

PAT (Advanced Level) 1032. Sharing (25)的相关文章

【PAT甲级】1032 Sharing (25分)

1032 Sharing (25分) 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 a

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT (Advanced Level) 1003. Emergency (25)

最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using nam

PAT (Advanced Level) 1029. Median (25)

scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<queue> #include<iostream> #include<algorithm> using namespace std; const int maxn=1000000+10; int a[maxn],b

PAT (Advanced Level) 1070. Mooncake (25)

简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; double eps=1e-7

1032. Sharing (25)【链表】——PAT (Advanced Level) Practise

题目信息 1032. Sharing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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.

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c