题目描写叙述

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;
}

最新文章

  1. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  2. hibernate 返回新插入数据的Id
  3. 这几天对Redis的初探,写一个阶段性的东西
  4. 判断UpLoader是否安装了Flash
  5. CRM PrincipalObjectAccess(POA)
  6. work8
  7. Build Firefox 编译Firefox
  8. unity3d中的http通信 二
  9. C#基础学习第一天(.net菜鸟的成长之路-零基础到精通)
  10. ASP.NET MVC下的异步Action的定义和执行原理
  11. (转)python struct简介
  12. 如何截取url中的各个参数?
  13. Performance tool httperf
  14. python 时间戳 datetime string 转换
  15. JS逗号、冒号与括号
  16. 打印进度条&gt;&gt;&gt;&gt;
  17. ubantu中执行docker免sudo方法
  18. Tikhonov regularization 吉洪诺夫 正则化
  19. 锚点的animate使用过程中定位不准确的问题小记
  20. 快速搭建完整zabbix3.4

热门文章

  1. Python-约瑟夫环
  2. struts向网页输出图片验证码
  3. Linux下二进制文件安装MySQL
  4. pycharm的一些操作指令和技巧
  5. Python+unittest发送测试报告
  6. SQL indexOf、lastIndexOf
  7. P1410 子序列 (动态规划)
  8. 记一次Jenkins 打包异常 ERROR: Exception when publishing, exception message [Failure]
  9. bzoj 5055: 膜法师 树状数组+离散
  10. zoj 2562 反素数