【PAT】1032 Sharing (25)(25 分)
1032 Sharing (25)(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 (<= 10^5^), 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
使用静态链表,设置一个标志位,当该结点出现在第一个链表中时,标志位为true,然后遍历第二个链表,当结点中的标志位为true时,即为寻找的相同后缀的第一个地址
C++代码如下:
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 100010
struct word {
char ch;
int next;
bool l=false;
}w[maxn]; int main() {
int add1, add2, n;
scanf("%d %d %d", &add1, &add2, &n);
int start, end;
char c;
for (int i = ; i < n; i++) {
scanf("%d %c %d", &start, &c, &end);
w[start].ch = c;
w[start].next = end;
}
for (int i = add1; i != -; i = w[i].next)
w[i].l = true;
int p;
for (p = add2; p != -; p = w[p].next)
if (w[p].l == true) break;
if (p != -) printf("%05d\n", p);
else printf("-1\n");
return ;
}
【PAT】1032 Sharing (25)(25 分)的更多相关文章
- PAT 1032 Sharing[hash][链表][一般上]
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- PAT 1032 Sharing (25分) 从自信到自闭
题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...
- PAT 1032. Sharing
其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- 1032 Sharing (25分)
1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...
- 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 ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT 1085 PAT单位排行(25)(映射、集合训练)
1085 PAT单位排行(25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤105),即考生人数.随 ...
- 怎么设置BarTender中二维码大小为25*25
有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...
随机推荐
- Oil Skimming HDU - 4185(匹配板题)
Oil Skimming Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- MT【81】含参数三次函数因式分解
解答: 评:这题实质上是对关于$x$的三次函数进行了一个因式分解.这种把$a$看成主元的技巧是初中处理高次的因式分解的常用技巧.如果用三次求导去做计算量比较大,要计算极值.
- 【刷题】BZOJ 3724 PA2014Final Krolestwo
Description 你有一个无向连通图,边的总数为偶数. 设图中有k个奇点(度数为奇数的点),你需要把它们配成k/2个点对(显然k被2整除).对于每个点对(u,v),你需要用一条长度为偶数(假设每 ...
- NOIP2018 No regrets youth
NOIP2018在即,20181009总结一些易错的知识点和解题方法 ——by ljc20020730 HGOI NOIP2018 No regrets youth ! NOIP2018 No reg ...
- 【bzoj1040】 ZJOI2008—骑士
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 (题目链接) 题意 一个基环森林,从中选出不相邻的若干个点使得这些点的点权和最大. Solut ...
- SpringBoot中使用SpringDataJPA
SpringDataJPA的使用 JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范. 为Java开发人员提供了一种对象/关联映射工具来管理Java ...
- 将句子表示为向量(上):无监督句子表示学习(sentence embedding)
1. 引言 word embedding技术如word2vec,glove等已经广泛应用于NLP,极大地推动了NLP的发展.既然词可以embedding,句子也应该可以(其实,万物皆可embeddin ...
- C#获取文件超大图标256*256(转)
从Bing搜索得到,保存于此 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 【Linux】MySQL安装及允许远程访问
安装环境/工具 Linux( centOS 版) MySQL(MySQL-5.6.28-1.el7.x86_64.rpm-bundle.tar版) 安装步骤 1.解压mysql安装文件 命令:tar ...
- ngx_lua_API 指令详解(二)ngx.re.match/find/gmatch/sub/gsub指令集合
1.先来个官方的ngx.re.match location /ngx_re_match { default_type text/html; content_by_lua_block { local m ...