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

//这个代码可以在牛客网上AC,但是在pat上有最后两个测试点通不过,只得了20分,问题在哪里呢?

#include <iostream>
#include <vector>
#include<stdio.h>
#include <algorithm>
using namespace std;
int node[]; int main()
{
int f,t,no;
int addr,next,outs;
char ch;
bool flag=false;
while(scanf("%d%d%d",&f,&t,&no)!=-)
{
for(int i=; i<no; i++)
{
scanf("%d %c %d",&addr,&ch,&next);
if(next!=-)
{
if(node[next]==)
node[next]=;
else
{
outs=next;
flag=true;
}
}
}
if(!flag)
printf("-1\n");
else{
printf("%5d\n",outs);
flag=false;
}
} return ;
}

//这个我甚至都没存储什么信息,因为一个节点如果在next的位置出现了2次,那么肯定就是那个公共的呀,还有什么异议吗?这可能就是我在pat上通不过的原因,找一下。

#include <iostream>
#include <vector>
#include<stdio.h>
#include <algorithm>
using namespace std;
int node[]; int main()
{
int f,t,no;
int addr,next,outs=-;
char ch;
bool flag=false;
while(scanf("%d%d%d",&f,&t,&no)!=-){
node[f]=;
node[t]=;
for(int i=; i<no; i++)
{
scanf("%d %c %d",&addr,&ch,&next);
if(next!=-)
{
if(node[next]==)
node[next]=;
else if(outs==-)
{
outs=next;
flag=true;
}
}
}
if(f==t){
printf("%05d",f);continue;//这个是针对测试点4,
//当起始地址一样时。
}
if(!flag)
printf("-1");
else{
printf("%05d",outs);
outs=-;
flag=false;
}
}
return ;
}

//应该是不行了,如果用只存next来做,没找到为什么测试点2和测试点5过不去,好绝望。先就这样吧。(有一个博客里是说,如果只判断next就不能判断出干扰点,而我想不出来干扰点是什么。)

大佬们的代码都是hash+模拟链表,先都把所有的点读入, 在第一条链表中出现的进行标记,第二条中出现的如果在第一条中已经出现了,那么就是这个点,

代码来自:https://www.liuchuo.net/archives/2113

#include <cstdio>
using namespace std;
struct NODE {
char key;
int next;
bool flag;
}node[];
int main() {
int s1, s2, n, a, b;
scanf("%d%d%d", &s1, &s2, &n);
char data;
for(int i = ; i < n; i++) {
scanf("%d %c %d", &a, &data, &b);
node[a] = {data, b, false};//结构体居然可以这么初始化。
}
for(int i = s1; i != -; i = node[i].next)
node[i].flag = true;//这样就可以模拟单链表的遍历
for(int i = s2; i != -; i = node[i].next) {
if(node[i].flag == true) {
printf("%05d", i);
return ;
}
}
printf("-1");
return ;
}

PAT 1032 Sharing[hash][链表][一般上]的更多相关文章

  1. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  2. 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 ...

  3. PAT 1032. Sharing

    其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...

  4. PAT 1032 Sharing (25分) 从自信到自闭

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

  5. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  6. 【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 ...

  7. 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 ...

  8. Hash 哈希(上)

    Hash 哈希(上) 目录 Hash 哈希(上) 简介 Hash函数的构造 取余法 乘积取整法 其他方法 冲突的处理 挂链法 开放定址法 线性探查法 二次探查法 双哈希法 结语 简介 Hash,又称散 ...

  9. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

随机推荐

  1. SQL Server 索引结构及其使用(二)

    作者:freedk 一.深入浅出理解索引结构 改善SQL语句 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select ...

  2. 【Java基础系列】Java IO系统

    前言 创建好的输入/输出系统不仅要考虑三种不同种类的IO系统(文件,控制台,网络连接)还需要通过大量不同的方式与他们通信(顺序,随机访问,二进制,字符,按行,按字等等). 一.输入和输出 Java的I ...

  3. 【Eclipse】启动时报错:No Java virtual machine (已解决)

    在 Ubuntu 上下了个最新的 Eclipse ,解压后运行报这样的错误: 当然 JDK 的安装及环境变量的配置是没有问题的.使用 java -version 查询本机的JDK版本是可以的. 如果不 ...

  4. jQuery弹出层插件大全

    1.thickbox 目前用的比较多的,最新版本是thickbox3.1 下载地址:http://jquery.com/demo/thickbox/#examples 2.colorBox 官方网站: ...

  5. 高性能Android应用开发

  6. java.util.concurrent.Future Basics

    Hereby I am starting a series of articles about future concept in programming languages (also known ...

  7. isolinux.cfg 文件是干什么的

    1.   首先光盘镜像也就是iso文件采用的是“ISO 9660 ”文件系统 . cd上的文件都存在这个简单的iso文件系统里,linux可以用mount  -o loop 直接把*.iso文件mou ...

  8. 一辈子只有1次成为BAT的机会,你如何把握?

    本文转自:http://www.fmi.com.cn/index.php?m=content&c=index&a=show&catid=9&id=614308 感谢作者 ...

  9. 【咸鱼教程】Wing动画编辑器创建精美(一般-_-)开场动画

    游戏中会用着一些简单的动画,公司一般使用的dragonbones制作,导出二进制格式或者MC来使用.感觉一些简单动画直接使用动画编辑器更加简便些. 引擎版本:5.0.14wing版本:4.1.0 一 ...

  10. ALTERA FPGA Quartus 指定memory综合使用 M4K块

    最近遇到个问题, 使用二位数组方式定义了一个RAM ,但是软件每次 都是使用逻辑单元综合 这块memory  , 在ALTERA的网页上 找到了 方法,,在定义的 memory前面加一句画 (* ra ...