USACO 2.3 Longest Prefix(乱搞)

这个题目纠结的输入啊,根本不知道如何读入啊。借鉴一下别人的读入,然后写了个DFS,一直挂在最后一组数据上了。。。记忆化了一下还是挂了,看别人读入的时候,看过别人的思路,然后换思路+优化后,终于过了。。。用字典树应该会更快,用map水过了。

 1 /*
 2  ID: cuizhe
 3  LANG: C++
 4  TASK: prefix
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <cmath>
10 #include <algorithm>
11 #include <map>
12 using namespace std;
13 char str[200012];
14 int o[200012];
15 map <string,int> p;
16 int main()
17 {
18     int len,n,i,j,ans;
19     char s[100];
20     freopen("prefix.in","r",stdin);
21     freopen("prefix.out","w",stdout);
22     for(;;)
23     {
24         scanf("%s",s);
25         if(s[0] == '.') break;
26         p[s] = 1;
27     }
28     n = 0;
29     while(scanf("%s",str + n) == 1)
30     {
31         n += strlen(str + n);
32     }
33     len = strlen(str);
34     o[0] = 1;
35     ans = 0;
36     for(i = 0; i < len; i ++)
37     {
38         if(o[i])
39         {
40             if(ans < i)
41             ans = i;
42             for(j = 0; j <= 9; j ++)
43             {
44                 s[j] = str[i+j];
45                 if(!o[j+1+i])
46                 {
47                 s[j+1] = '\0';
48                 if(p[s]) o[j+1+i] = 1;
49                 }
50             }
51         }
52     }
53     if(o[len]) ans = len;
54     printf("%d\n",ans);
55     return 0;
56 }
posted @ 2012-11-12 19:03  Naix_x  阅读(174)  评论(0编辑  收藏  举报