【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符号与版本选项来帮忙.本文小编就来给大家详细讲 ...
随机推荐
- CRM 报表导出excel时指定sheet名
如图所示,设置PageName即可: 这样导出excel时,sheet的名就有了:
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- BZOJ3193 [JLOI2013]地形生成 【dp】
题目链接 BZOJ3193 题解 注意\(key\)是小于 第一问,显然按高度降序排序,逐个插入 如果高度各不相同,那么之前插入的都比当前插入的\(i\)大,可插入的位置个数就确定了 由于存在高度相同 ...
- 前端学习 -- Css -- 选择器的优先级
当使用不同的选择器,选中同一个元素时并且设置相同的样式时,这时样式之间产生了冲突,最终到底采用哪个选择器定义的样式,由选择器的优先级(权重)决定优先级高的优先显示. 优先级的规则 内联样式 , 优先级 ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
- python学习(26)分析ajax请求抓取今日头条cosplay小姐姐图片
分析ajax请求格式,模拟发送http请求,从而获取网页代码,进而分析取出需要的数据和图片.这里分析ajax请求,获取cosplay美女图片. 登陆今日头条,点击搜索,输入cosplay 下面查看浏览 ...
- AngularJs分层结构小demo
后端mvc分层,前端也要分层才对嘛.分层的好处不言而喻.简直太清晰,容易维护.反正清爽的一逼.不信你看. 思路:分为controller层和service层.controller层再提取一个公共的层. ...
- Codeforces 923 B. Producing Snow
http://codeforces.com/contest/923/problem/B 题意: 有n天,每天产生一堆体积为Vi的雪,每天所有雪堆体积减少Ti 当某一堆剩余体积vi<=Ti时,体积 ...
- 2018年11月25日ICPC焦作站参赛总结
可能就这么退役了吧. 对这次ICPC还是比较有信心的,毕竟心态都放平和了. 路途很波折,热身赛还是赶上了. 等到了正赛的时候,开场看出了A题的签到,签到肯定是我来签的,11分钟签完了这道题之后,开始看 ...
- Nginx 服务器性能Bug和性能优化方案(真实经历)
一.遇到的问题 1.问题:本应该是3个ffmpeg ,但是怎么会有5个ffmpeg出现? 2.Lua脚本问题,一直写入日志,导致有大量的日志,这里的错误日志是直接写进nginx的error.log 日 ...