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

Hdu 1251 统计难题

时间:2015-08-03 10:05:35      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

错误代码:Memory Limit Exceeded

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 typedef struct node
 5 {
 6     int sum;
 7     node *next[26];
 8 }*tree,t;
 9 tree root;
10 void Insert(char *str)
11 {
12     tree p=root,newnode;
13     for(; *str!=\0; str++)
14     {
15         if(p->next[*str-a]!=NULL)
16         {
17             p=p->next[*str-a];
18             p->sum++;
19         }
20         else
21         {
22             newnode = new node();
23             for(int i=0; i<26; i++)
24                 newnode->next[i]=NULL;
25             newnode->sum=1;
26             p->next[*str-a]=newnode;
27             p=newnode;
28         }
29     }
30 }
31 int Find(char *str)
32 {
33     tree p=root;
34     for(; *str!=\0; str++)
35     {
36         if(p->next[*str-a]!=NULL)
37             p=p->next[*str-a];
38         else
39             return 0;
40     }
41     return p->sum;
42 }
43 int main()
44 {
45     char str[15];
46     root = new node();
47     for(int i=0; i<26; i++)
48         root->next[i]=NULL;
49     root->sum=0;
50     while(gets(str),strcmp(str,"")!=0)
51         Insert(str);
52     while(gets(str),strcmp(str,"")!=0)
53         printf("%d\n",Find(str));
54     return 0;
55 }

 

 

---恢复内容结束---

Hdu 1251 统计难题

标签:

原文地址:http://www.cnblogs.com/cn-blog-cn/p/4698560.html

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