zoj 1109 Language of FatMouse

38473 단어 language
Time Limit: 10 Seconds     
Memory Limit: 32768 KB
이 문제는 간단한 사전 나무의 문제입니다. 하지만 맵으로도 물을 통과할 수 있습니다. 하지만 저는 사전 나무를 사용해서 자신을 단련하고 싶습니다!사전을 쓰면 좀 더 빠를 수 있다
맵 용기의 코드로: (1970ms)

   
     
1 #include < iostream >
2 #include < map >
3 #include < string >
4   using namespace std;
5 int main()
6 {
7 map < string , string > m;
8 char c[ 40 ],a[ 20 ],b[ 20 ];
9 int i,index,len,j;
10 while ( 1 )
11 {
12 gets(c);
13 j = 0 ;
14 len = strlen(c);
15 if (len == 0 )
16 break ;
17 for (i = 0 ;i < len;i ++ )
18 if (c[i] == ' ' )
19 {
20 index = i;
21 break ;
22 }
23 else
24 a[i] = c[i];
25 for (i = index + 1 ;i < len;i ++ )
26 b[j ++ ] = c[i];
27 b[j] = ' \0 ' ;
28 a[index] = ' \0 ' ;
29 m[b] = a;
30 }
31 map < string , string > ::iterator it;
32 while (scanf( " %s " ,c) != EOF)
33 {
34 it = m.find(c);
35 if (it != m.end())
36 {
37 cout << ( * it).second << endl;
38 }
39 else
40 cout << " eh " << endl;
41 }
42 return 0 ;
43 }
44

사전 트리 코드: (130ms)

   
     
1 #include < stdio.h >
2 #include < stdlib.h >
3 #include < string .h >
4   #define MAX 100005
5 typedef struct node
6 {
7 struct node * next[ 30 ];
8 char s[ 10 ];
9 int h;
10 } * Trie,T;
11 void init(Trie & root)
12 {
13 int i;
14 root = (Trie)malloc( sizeof (node));
15 root -> h = 0 ;
16 for (i = 0 ;i < 30 ;i ++ )
17 {
18 root -> next[i] = NULL;
19 }
20 }
21 void insert( char path[], char s[],Trie root)
22 {
23 int len,i,j;
24 len = strlen(path);
25 for (i = 0 ;i < len;i ++ )
26 {
27 if (root -> next[path[i] - ' a ' ] == NULL)
28 {
29 Trie t = (Trie)malloc( sizeof (T));
30 for (j = 0 ;j < 30 ;j ++ )
31 {
32 t -> next[j] = NULL;
33 t -> h = 0 ;
34 }
35 root -> next[path[i] - ' a ' ] = t;
36 }
37 root = root -> next[path[i] - ' a ' ];
38 }
39 root -> h = 1 ;
40 strcpy(root -> s,s);
41 }
42 void find( char s[],Trie root)
43 {
44 int len,i;
45 len = strlen(s);
46 for (i = 0 ;i < len;i ++ )
47 {
48 if (root -> next[s[i] - ' a ' ] != NULL)
49 root = root -> next[s[i] - ' a ' ];
50 else
51 break ;
52 }
53 if (i == len && root -> h == 1 )
54 printf( " %s " ,root -> s);
55 else
56 printf( " eh " );
57 }
58 int main()
59 {
60 Trie root;
61 char a[ 20 ],b[ 20 ],c[ 40 ];
62 int len,i,j,index;
63 init(root);
64
65 while ( 1 )
66 {
67 gets(c);
68 j = 0 ;
69 len = strlen(c);
70 if (len == 0 )
71 break ;
72 for (i = 0 ;i < len;i ++ )
73 if (c[i] == ' ' )
74 {
75 index = i;
76 break ;
77 }
78 else
79 a[i] = c[i];
80 for (i = index + 1 ;i < len;i ++ )
81 b[j ++ ] = c[i];
82 b[j] = ' \0 ' ;
83 a[index] = ' \0 ' ;
84 insert(b,a,root);
85 }
86 while (scanf( " %s " ,c) != EOF)
87 {
88 find(c,root);
89 printf( "
" );
90 }
91 return 0 ;
92 }
93
94

좋은 웹페이지 즐겨찾기