博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1032. Sharing (25)
阅读量:6921 次
发布时间:2019-06-27

本文共 2375 字,大约阅读时间需要 7 分钟。

题目链接:

题目:

1032. Sharing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<= 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}, andNext 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 967890 i 0000200010 a 1234500003 g -112345 D 6789000002 n 0000322222 B 2345611111 L 0000123456 e 6789000001 o 00010
Sample Output 1:
67890
Sample Input 2:
00001 00002 400001 a 1000110001 s -100002 a 1000210002 t -1
Sample Output 2:
-1

分析:

两个链表。找到它们相交的第一个节点。

注意:

可能存在多个字串。而且可能有其它非题目中两字串的节点存在。而且注意最后是要输出%05d的

AC代码:

#include
#include
#include
using namespace std;int nodes[100001];int real_nodes[100001];int main(void){ //freopen("F://Temp/input.txt", "r", stdin); int s1, s2, n; scanf("%d %d %d", &s1, &s2, &n); int s, t; char c; for (int i = 0; i < 100001; i++){ nodes[i] = 0; real_nodes[i] = 0; } for (int i = 0; i < n; i++){ scanf("%d %c %d", &s, &c, &t); nodes[s] = t; } int start = s1; int end; while (start != -1){ real_nodes[start] = 1; start = nodes[start]; }//找到第一个链表的全部节点,并置为1 start = s2; bool find = false; while (start != -1){ if (real_nodes[start] != 0){//假设第二个链表中地址已经有记录,则就是第一个公共节点 printf("%.5d\n", start); return 0; } else { real_nodes[start] = nodes[start]; start = nodes[start]; } } puts("-1"); return 0;}

截图:

——Apie陈小旭

你可能感兴趣的文章
laravel数据库事务回滚
查看>>
安卓问题集锦
查看>>
关于zend stadio 10.1 无法追踪函数的解决办法
查看>>
Java常用英文缩写术语全称
查看>>
深入理解RunLoop(三)
查看>>
基于番茄土豆的scrum工时估计方法尝试
查看>>
ssh 连接很慢的解决办法
查看>>
搭建springmvc+mybaits+maven碰掉问题(如何修改Maven的JDK版本)
查看>>
Thinkphp3.2.3在SQL执行错误时查看SQL语句
查看>>
mupdf将pdf文件中的某页导出成图片
查看>>
自定义内置 tomcat,自定义spring boot 内置tomcat的404页面
查看>>
CentOS7安装Java JDK 1.8
查看>>
Bluetooth 4.0 mio alpha watch 心率监护应用 2
查看>>
linux之Find命令总结
查看>>
Nginx开启目录浏览
查看>>
单点登陆的实现(一)
查看>>
智慧生态何以成海尔颠覆性网器?
查看>>
你该知道的Java注释!
查看>>
13个web网站访问速度测速服务
查看>>
思科AP关联日志
查看>>