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 (≤), 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 −.
Then N lines follow, each describes a node in the format:
Address Data Next
whereAddress
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
题目分析:对于第一个链表 把他们都置为访问过 在第二个链表中查找访问过的节点 (我怎么就想不出这样的解法呢。。。。)
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
struct Node{
char data;
int next;
bool flag;
}Nodes[];
int main()
{
int add1, add2,n,a,b;
char c;
cin >> add1 >> add2 >> n;
for (int i = ; i < n; i++)
{
cin >> a >> c >> b;
Nodes[a] = { c,b,false };
}
for (int i = add1; i != -; i = Nodes[i].next)
Nodes[i].flag = true;
for(int i=add2;i!=-;i=Nodes[i].next)
if (Nodes[i].flag == true)
{
printf("%05d", i);
return ;
}
cout << -;
return ;
}
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 let ...
- 1032 Sharing (25分)
1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...
- 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 (25 分)
题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...
- 【PAT】1032 Sharing (25)(25 分)
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- 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 ...
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- PAT Advanced 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 (25)-链表水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
随机推荐
- 01 搭建EasyMock环境
EasyMock 介绍 EasyMock是一个构建模拟数据的平台,也可以说是一个在线mockJs平台 EasyMock优势 省去配置.安装mockJs步骤,解决多人协作Mock数据不互通问题 不需要在 ...
- 逐浪web无障碍与国际化以及全民族语言支持白皮书
北京时间2019年5月10日,领先的门户网站与WEB内核服务厂商--上海Zoomla!逐浪CMS团队发布其年度重榜产品:逐浪CMS全民族语言与国际版,体验站点:http://demo2.z01.com ...
- 使用twisted将mysql插入变成异步执行
python 异步MySQL存库 对于异步框架而言,这些延迟是无法接受的.因此, Twisted 提供了 twisted.enterprise.adbapi, 遵循DB-API 2.0协议的一个异 ...
- js Array方法总结
修改器方法(9) copyWithin(target: number, start: number, end?: number): this; // 浅复制数组的一部分到同一数组中的另一个位置,并返回 ...
- 建议14:灵活使用Arguments
Javascript支持Arguments机制.Arguments是一个为数组,可以通过数组下标形式获取该集合中传递给函数的参数值.例如:下面函数中,没有指定形参,但在函数体内通过Arguments对 ...
- pytorch的自动求导机制 - 计算图的建立
一.计算图简介 在pytorch的官网上,可以看到一个简单的计算图示意图, 如下. import torchfrom torch.autograd import Variable x = Variab ...
- File 关键词
getParent() 获取父路径 getAbsoluteFile 获取绝对路径 length() 获得文件的字节数 getName() 获取路径中最后部分的名字 getPath() 获取整体路径. ...
- 机器学习- Attention Model结构解释及其应用
概述 Attention Model 的出现,在sequence model的领域中算是一个跨时代的事件.在Many-to-Many的sequence model中,在decoder network中 ...
- Windows主机与centOS虚拟机之间"ping不通"
为什么要遇到这个问题 这是我重新安装centOS7.5虚拟机之后遇到的问题——我需要安装一个SecureCRT工具,结果主机与虚拟机没有ping通. 在安装这个工具之前需要进行主机与虚拟机的相互pin ...
- pc 媒体查询
PC端 按屏幕宽度大小排序(主流的用橙色标明) 分辨率 比例 | 设备尺寸 1024*500 (8.9寸) 1024*768 (比例4:3 | 10.4寸.12.1寸.14.1寸.15寸; ) ...