题目描写叙述

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

输入描写叙述:

Each input file contains one test case.  For each case, the first line contains two addresses of nodes and a positive N (<= 105), 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.

输出描写叙述:

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.

输入样例:

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

输出样例:

67890
//最初的解法低效。且有未知错误
/*思路例如以下:
从最后面開始找。直到存在某个前驱有不止一个后继指向它,则结束*/
#include <iostream>
#include <iomanip> using namespace std; typedef struct Node
{
int addressPre,addressPost;
char data;
}; int main()
{
int a1,a2,N,i,j,k,ans;
while(cin>>a1>>a2>>N)
{
Node *node=new Node[N];
for(i=0;i<N;i++)
cin>>node[i].addressPre>>node[i].data>>node[i].addressPost;
/* for(i=0;i<N;i++)
cout<<node[i].addressPre<<" "<<node[i].data<<" "<<node[i].addressPost<<endl;
*/ k=-1;
while(1)
{
ans=0;
for(i=0;i<N;i++)
{
if(k==node[i].addressPost)
{
ans++;
j=i;
}
}
if(ans>1)
break;
else
k=node[j].addressPre;
}
cout <<setfill('0')<< setw(5)<<k<<endl;
}
return 0;
}

正解

#include <iostream>
#include <iomanip>
#include <vector> using namespace std; typedef struct Node
{
int addressPre,addressPost;
char data;
}; vector<Node> list1;
vector<Node> list2; Node node[100010]; int main()
{
int a1,a2,N,i,j,k;
int l1,l2;
Node tNode;
while(cin>>a1>>a2>>N)
{
for(i=0;i<N;i++)
{
cin>>tNode.addressPre>>tNode.data>>tNode.addressPost;
node[tNode.addressPre]=tNode;
}
l1=a1;
l2=a2;
while(l1!=-1)
{
list1.push_back(node[l1]);
l1=node[l1].addressPost;
}
while(l2!=-1)
{
list2.push_back(node[l2]);
l2=node[l2].addressPost;
}
for(i=0,k=0;i<list1.size();i++)
{
for(j=0;j<list2.size();j++)
{
if(list1[i].addressPre==list2[j].addressPre)
{
k=1;
break;
}
}
if(k)
break;
}
if(k)
cout <<setfill('0')<< setw(5)<<list1[i].addressPre<<endl;
else
cout<<-1<<endl; }
return 0;
}

PAT 1003 Sharing (25)的更多相关文章

  1. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  2. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  3. PAT 1003 Emergency (25分)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  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 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  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. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  9. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

随机推荐

  1. Python从文件中读取数据

    一.读取整个文件内容 在读取文件之前,我们先创建一个文本文件resource.txt作为源文件. resource.txt my name is joker, I am 18 years old, H ...

  2. url编码&&PHP大法&&这个看起来有点简单&&HTML 中有用的字符实体

    URL编码 Url编码通常也被称为百分号编码(Url Encoding,also known as percent-encoding),是因为它的编码方式非常简单,使用%百分号加上两位的字符——012 ...

  3. eclipse中新建maven项目无法添加src/main/java问题

    eclipse创建maevn web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结构,添加 ...

  4. 快速入门Numpy

    教你十分钟学会使用numpy. 简单介绍一下numpy的话,这就是一个基于多维数组的python科学计算的核心库. 基本信息 # 一般用np作为numpy的缩写 import numpy as np ...

  5. gitlab之gitlab-ci自动部署

    简介 gitlab-ci全称是gitlab continuous integration的意思,也就是持续集成.中心思想是当每一次push到gitlab的时候,都会触发一次脚本执行,然后脚本的内容包括 ...

  6. goland 快键键整理及注册

    https://my.oschina.net/lemos/blog/1358731 http://idea.lanyus.com/

  7. Spark——为数据分析处理提供更为灵活的赋能

    本文来自网易云社区 作者:王佳楠 一.概述 现如今在大规模数据处理分析的技术领域中,Hadoop及其生态内的各功能组件占据了绝对的统治地位.Hadoop原生的MapReduce计算框架由于任务抽象简单 ...

  8. IOS 自动布局-UIStackPanel和UIGridPanel(四)

    为什么说scrollview的自动化布局是难点? 对scrollview做自动化布局,无非就是想对scrollview里面的subviews来做自动化布局.但是scrollview里面的subview ...

  9. 学习笔记7——wp版本更新需要注意的问题

    平时开发时应该避免修改wp的核心代码, 因为在升级wp版本时,核心代码都会被覆盖, wp升级时只有wp-content文件夹不会被覆盖.

  10. ospf 提升 二 ---LSA

    ospf ABR和ASBR的区别 官方建议中大型网络的规模参考   根据spf算法   而不是路由器的硬件性能强弱 a ABR最多关联3个区域 b 单区域内路由器最多50台 c 一台运行ospf的路由 ...