题目1468:Sharing
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目1468:Sharing相关的知识,希望对你有一定的参考价值。
题目描述:
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.
You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).
样例输入:
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 00001 00002 4 00001 a 10001 10001 s -1 00002 a 10002 10002 t -1
1 #include<stdio.h> 2 #include<cstring> 3 #include<algorithm> 4 const int MAXN = 100050; 5 int main(){ 6 int pos[MAXN]; 7 int flag[MAXN]; 8 int times[MAXN]; 9 int N; 10 int tmp1, tmp2; 11 char ch; 12 while(~scanf("%d%d%d",&tmp1,&tmp2,&N)){ 13 memset(pos,0,sizeof(pos)); 14 memset(flag,0,sizeof(pos)); 15 memset(times,0,sizeof(pos)); 16 pos[0]=tmp1; 17 flag[tmp1]++; 18 if(tmp2==tmp1) 19 flag[tmp1]++; 20 while(N--){ 21 scanf("%d %c %d",&tmp1,&ch,&tmp2); 22 pos[tmp1]=tmp2; 23 if(tmp2!=-1) 24 flag[tmp2]++; 25 } 26 tmp1=0; 27 bool ans=false; 28 while(pos[tmp1]!=-1){ 29 if(flag[tmp1]==2){ 30 printf("%05d\n",tmp1); 31 ans=true; 32 break; 33 } 34 tmp1=pos[tmp1]; 35 } 36 if(!ans) 37 printf("-1\n"); 38 } 39 } 40 41 /************************************************************** 42 Problem: 1468 43 User: blueprintf 44 Language: C++ 45 Result: Accepted 46 Time:180 ms 47 Memory:2116 kb 48 ****************************************************************/
两个链表,求其公共部分,并没有真的打链表,用了三个数组,一个hash表,一个记录访问次数,
沿着某一个连边走下去,访问次数为2的既是公共部分的起点,
结果输出需要注意前导0,%05d,用0填充,%5d,用空格填充
以上是关于题目1468:Sharing的主要内容,如果未能解决你的问题,请参考以下文章