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=+;
- struct Node
- {
- int id;
- char c;
- int nx;
- } node[maxn];
- int add1,add2,n;
- int s1[maxn],s2[maxn];
- int len1=,len2=;
- int m[maxn];
- int main()
- {
- memset(m,-,sizeof m);
- scanf("%d%d%d",&add1,&add2,&n);
- for(int i=; 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=; i<=n; i++)
- {
- if(node[i].nx==-) continue;
- node[i].nx=m[node[i].nx];
- }
- memset(s1,-,sizeof s1);
- memset(s2,-,sizeof s2);
- int now=m[add1];
- while()
- {
- if(now==-) break;
- s1[len1++]=now;
- now=node[now].nx;
- }
- now=m[add2];
- bool flag[maxn];
- memset(flag,,sizeof flag);
- int fail=;
- while()
- {
- if(now==-) break;
- if(flag[now]==)
- {
- fail=;
- break;
- }
- flag[now]=;
- s2[len2++]=now;
- now=node[now].nx;
- }
- if(fail==)
- {
- printf("-1\n");
- }
- else
- {
- len1--;
- len2--;
- while(len1>=&&len2>=)
- {
- if(s1[len1]==s2[len2])
- {
- len1--;
- len2--;
- }
- else break;
- }
- len1++;
- len2++;
- if(s1[len1]==-) printf("-1\n");
- else printf("%05d\n",node[s1[len1]].id);
- }
- return ;
- }
PAT (Advanced Level) 1032. Sharing (25)的更多相关文章
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT (Advanced Level) 1029. Median (25)
scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- PAT (Advanced Level) 1003. Emergency (25)
最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...
- PAT (Advanced level) 1003. Emergency (25) Dijkstra
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- 【PAT甲级】1032 Sharing (25 分)
题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
随机推荐
- 第一次提交代码到github时经常遇到的问题
最近两年在OpenStack方面做了一些工作,写了一些实验性的plugin. 本着Open Source的共享精神,想尝试提交到github,以便他人能下载使用. 当你注册完github帐号之后,点击 ...
- 在一台电脑上运行两个或多个tomcat
在一台电脑上运行多个tomcat 在本例中,使用两个tomcat做示例 工具/原料 tomcat 安装好jdk,并且配置好环境变量 方法/步骤 首先去apache下载一个tomcat, ...
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 忘记mysql密码
[root@mysql-db03 ~]# mysql -uroot -poldboy123Warning: Using a password on the command line interface ...
- pylint安装失败的解决方法
原文链接http://www.cnblogs.com/Loonger/p/7815335.html 使用命令pip3 install pylint安装pylint是出现错误.查了一圈也找不到答案.仔细 ...
- html自己写响应式布局(说起来很高大上的样子,但是其实很简单)
第一步,打开电脑中安装的Sublime Text3,新建demo文件夹用来存放文件,在里面新建一个HTML文件,通过Tab快捷键迅速创建一个HTML模板,并命名标题. 第二步,在Body标签里添加三个 ...
- Python中Pickle模块的dump()方法和load()方法
Python中的Pickle模块实现了基本的数据序列与反序列化. 经常遇到在Python程序运行中得到了一些字符串.列表.字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就 ...
- 关于sigleton模式
单例模式的要点有三个:一是某个类只能有一个实例:二是它必须自行创建这个实例:三是它必须自行向整个系统提供这个实例. 从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,二是类定义中 ...
- treeTable的使用(ajax异步获取数据,动态渲染treeTable)
一.展示效果(treetable基本样式https://www.cnblogs.com/shuihanxiao/p/10413454.html) 二.html文件(若一个页面又多个treetable, ...
- JavaEE-04 数据源配置
学习要点 JNDI 数据库连接池 完成新闻发布系统数据库连接池 JNDI 说明 JNDI(Java Naming and Directory Interface),中文翻译为Java命名与目录接口,是 ...
- ping ip
def ip_and_time(): """ get ip to ping from ip.txt then return two list , each ip that ...