题目描写叙述

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. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  3. H.264编码profile & level控制

    背景知识 先科普一下profile&level.(这里讨论最常用的H264) H.264有四种画质级别,分别是baseline, extended, main, high:  1.Baseli ...

  4. Leetcode 397.整数替换

    整数替换 给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n.2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n.n 变为 1 所需的最小替换次数是 ...

  5. 九度oj 题目1137:浮点数加法

    题目描述: 求2个浮点数相加的和 题目中输入输出中出现浮点数都有如下的形式:P1P2...Pi.Q1Q2...Qj对于整数部分,P1P2...Pi是一个非负整数 对于小数部分,Qj不等于0 输入: 对 ...

  6. HDU 3949 XOR ——线形基 高斯消元

    [题目分析] 异或空间的K小值. 高斯消元和动态维护线形基两种方法都试了试. 动态维护更好些,也更快(QAQ,我要高斯消元有何用) 高斯消元可以用来开拓视野. 注意0和-1的情况 [代码] 高斯消元 ...

  7. BZOJ3130 [Sdoi2013]费用流 【网络流 + 二分】

    题目 Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识. 最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量.一个合法的网络流方案必须满足:(1)每 ...

  8. Garbage First介绍

    本文摘自<构建高性能的大型分布式Java应用>一书,Garbage First简称G1,它的目标是要做到尽量减少GC所导致的应用暂停的时间,让应用达到准实时的效果,同时保持JVM堆空间的利 ...

  9. HDU4768:Flyer [ 二分的奇妙应用 好题 ]

    传送门 Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. php——解决中文乱码问题

    一般写代码的时候遇到中文乱码的问题还是比较烦躁的,下面是我总结的几种中文乱码的解决办法: 1:php在头部设置header设置编码方式: header('Content-type:text/html; ...