pat-a1032. Sharing (25)
来源:互联网 发布:旅游 本质知乎 编辑:程序博客网 时间:2024/06/05 09:41
同一个字母会有不同的前后地址。。。不能像上个反转链接表一样使用map
#include<cstdio>#include<cstring>#include<map>using namespace std;struct node{int l;char c;};node no[1000000];char t1[100010];char t2[100010];int visit[1000010];int main(){int a,b,c,x,z;char y;scanf("%d%d%d",&a,&b,&c);for(int i=0;i<c;++i){scanf("%d %c %d",&x,&y,&z);no[x].c=y;no[x].l=z;}int t=0,k=0;while(a!=-1){visit[a]=1;t1[t++]=no[a].c;a=no[a].l;}while(b!=-1){if(visit[b]) break;t2[t++]=no[b].c;b=no[b].l;}if(b==-1) printf("-1\n");else printf("%05d\n",b);return 0;}
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 00010Sample Output 1:
67890Sample Input 2:
00001 00002 400001 a 1000110001 s -100002 a 1000210002 t -1Sample Output 2:
-1
- 【PAT】A1032. Sharing (25)
- PAT A1032. Sharing (25)
- pat-a1032. Sharing (25)
- PAT [A1032]-Sharing
- A1032. Sharing (25)
- A1032.Sharing
- PAT-A1032
- PAT a1032题解
- 1032. Sharing (25)-PAT
- 【PAT】1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT:1032. Sharing (25)
- PAT 1032. Sharing (25)
- pat 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1003 Sharing (25)
- 用pdflatex编译遇到K font txsy could not be created问题
- c++中size_t和size_type之间的关系
- Vue.js使用vue-router构建单页应用
- Java二维数组初始化
- exports 和 module.exports 的区别
- pat-a1032. Sharing (25)
- 3.4、#pragma预处理分析
- POJ1190_生日蛋糕_深搜
- react-core-image-upload:轻量级的图片上传裁剪组件
- 深入理解java虚拟机(二)
- caffe中出现这种错误:由于/usr/bin 不在PATH 环境变量中,故无法找到该命令。
- 状态模式
- 复杂的整数划分
- 前端开发工具Sublime Text3的使用配置