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 ...
随机推荐
- 爬虫使用中间代理人 fiddl...,charles,mitmproxy 设置
一般的设置在网上就能找到(端口,ip啥的) 但是难点是关于安卓手机证书 在网上找到的几种方法,一种是在app源码中添加设置让手机app同意你下载安装的证书,另一种则是root_adb 安装证书 但是太 ...
- SQL之开窗函数详解--可代替聚合函数使用
在没学习开窗函数之前,我们都知道,用了分组之后,查询字段就只能是分组字段和聚合的字段,这带来了极大的不方便,有时我们查询时需要分组,又需要查询不分组的字段,每次都要又到子查询,这样显得sql语句复杂难 ...
- c++第一章1.6
测试已完成(bingo) 1 [单选题] 下面代码能够实现交换操作的函数有( ) A. swap(int a,int b) { int t=a;a=b;b=t;} B. swa ...
- 前后端分离项目采用Prerender的SEO优化流程
原文: https://blog.ccyws.cn/articles/4 一.概述 近年开发模式变化,新建Web站点采用前后端分离部署已经是大势所趋.但是,搜索引擎爬虫不会执行js脚本从后端加载数据, ...
- npm 安装包总结
1.前端可视化json:vue-json-viewer; 2.富文本编辑: vue-quill-editor; https://www.cnblogs.com/ZaraNet/p/1020922 ...
- SQLserver用Image格式储存图片
前言 最近项目更新一个新需求,要求把图片储存在SQLserver中,而不是储存在本地磁盘.很好,又有新东西可以学了. 正文 一.建表 这里大概建几个字段演示一下 CREATE TABLE [dbo]. ...
- 普通索引和唯一索引如何选择(谈谈change buffer)
假设有一张市民表(本篇只需要用其中的name和id_card字段,有兴趣的可以翻看“索引”篇,里面有建表语句) 每个人都有一个唯一的身份证号,且业务代码已经保证不会重复. 由于业务需求,市民需要按身份 ...
- VMware虚拟机安装Mac OS X 10.12
VMware Workstation Pro 14 安装Mac OS X 10.12 下面是所需要的补丁工具及镜像 VMware Workstation unlocker-master(OS X 虚拟 ...
- 保姆级教程!手把手教你使用Longhorn管理云原生分布式SQL数据库!
作者简介 Jimmy Guerrero,在开发者关系团队和开源社区拥有20多年的经验.他目前领导YugabyteDB的社区和市场团队. 本文来自Rancher Labs Longhorn是Kubern ...
- List集合概述和特点
List集合概述 有序集合(也称序列)用户可以精确控制列表的每一个元素的位置插入,用户可以通过整数索引访问元素,并搜索列表中的元素 与set集合不同,列表通常允许重复的元素 List集合的特点 有序: ...