码迷,mamicode.com
首页 > 其他好文 > 详细

题目1468:Sharing

时间:2016-06-27 01:30:11      阅读:145      评论:0      收藏: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.
技术分享

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

标签:

原文地址:http://www.cnblogs.com/blueprintf/p/5618915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!