PAT甲级 1032
来源:互联网 发布:c编程思想pdf 编辑:程序博客网 时间:2024/06/05 03:21
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 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 967890 i 0000200010 a 1234500003 g -112345 D 6789000002 n 0000322222 B 2345611111 L 0000123456 e 6789000001 o 00010Sample Output 1:
67890Sample Input 2:
00001 00002 400001 a 1000110001 s -100002 a 1000210002 t -1Sample Output 2:
-1
考点
- 静态链表
题解
- 用visit记录其中一个链表的地址。
- 再遍历另外一个链表,若地址已经visit过,则为相同的节点。
#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;#define MAXSIZE 100000+10int data[MAXSIZE];bool visit[MAXSIZE];int main(){ freopen("./in","r",stdin); int s1,s2,n; scanf("%d%d%d",&s1,&s2,&n); int i; int temp; char c; int nexts; for(i=0;i<n;i++){ scanf("%d %c %d",&temp,&c,&nexts); data[temp]=nexts; } for(i=s1;i!=-1;i=data[i]){ visit[i]=true; } for(i=s2;i!=-1;i=data[i]){ if(visit[i]){ printf("%05d",i); return 0; } } printf("-1"); return 0;}
- PAT(甲级)1032
- 浙大PAT甲级 1032
- PAT甲级1032
- 浙大PAT甲级-1032
- PAT甲级 1032
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- Ubuntu 14.04,samba 无法访问,指定网络名不再使用
- POJ1201-Intervals(差分约束)
- 3094. Quicksum
- mac 下安装mysql&启动
- Linux错误PYCURL ERROR 6
- PAT甲级 1032
- Android开源项目分类汇总
- 自定义的信号和槽实例
- CSS笔记
- typedef struct与struct的区别
- vue2.0安装和实例
- Android学习之创建自定义View(入门)
- Codeforces Round #392 (Div. 2) 758C Unfair Poll
- Postgresql递归查询