HUD Problem - 1251 统计难题
2015-08-09 14:20
281 查看
解题感悟:
提交时注意语言选择,这道题用G++提交 ,会爆内存(Memory Limit Exceeded),用c++提交可以AC .
这道题用一般方法(把单词全放在数组里检索)的话一般会超时(Time Limit Exceeded)
提交时注意语言选择,这道题用G++提交 ,会爆内存(Memory Limit Exceeded),用c++提交可以AC .
这道题用一般方法(把单词全放在数组里检索)的话一般会超时(Time Limit Exceeded)
#include<stdio.h> #include<string.h> #include<stdlib.h> #define LOCAL typedef struct node{ int count; struct node *next[26]; }tree; char str[12]; tree root; int size; void add_to_tree(char *str); int sum_num(char *str); int main(void){ #ifdef LOCAL freopen("datain.txt","r",stdin); #endif size = sizeof(tree); memset(&root,0,size); while(gets(str) && str[0]!='\0') add_to_tree(str); while(scanf("%s",str)!=EOF) printf("%d\n",sum_num(str)); return 0; } //add the word to the tree; void add_to_tree(char *str){ int len = strlen(str); tree *pointer = &root; pointer->count++; int i; for(i=0;i<len;++i){ if(pointer->next[str[i]-'a'] == NULL){ pointer->next[str[i]-'a'] = (tree *)malloc(size); memset(pointer->next[str[i]-'a'],0,size); } pointer = pointer->next[str[i]-'a']; pointer->count++; } } //find the number of words which contain this prefix; int sum_num(char *str){ int len=strlen(str); tree *pointer = &root; int i; for(i=0;i<len;++i){ pointer = pointer->next[str[i]-'a']; if(pointer==NULL) return 0; } return pointer->count; }
相关文章推荐
- leetcode 90: Subsets II
- Keil 重定向 printf
- 使用vs2015编译zlib1.28
- 链家地产页面抓取实验以及二手房信息统计概览
- 如何在面试中发现优秀程序员
- hdu5365 简单几何问题
- Python爬虫之抓取网络图片
- Start up using Task Scheduler
- IOS唤醒其他程序,程序间相互调用
- Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。
- 使用Android Studio 1.3 版本进行NDK开发 编译运行hello-jni项目及分析
- HDU 2102 A计划
- HDU 1207 汉诺塔II
- UIImageView 使图片圆形的方法
- 协议详解1——概要
- mootools upgrate from 1.2 to 1.3 or 1.4
- Hiding loaded driver with DKOM
- H5+App的页面事件定义,以及页面传值
- 第20章 多任务和多线程(2)
- Linux Per-cpu变量