hdu 1251 字典树
2015-06-08 14:55
197 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1251
MLE了无数次…用网上别人的代码还是MLE..最后换了种方法
MLE了无数次…用网上别人的代码还是MLE..最后换了种方法
#include <iostream> #include <cstring> #include <cstdio> using namespace std; struct node { int num; int next[26]; void ini() { num=0; memset(next,-1,sizeof(next)); } }a[3000000]; int p; inline void po() { a[p=0].ini(); } inline void insert(char *s) { int k=0; for(int i = 0; s[i]!='\0'; i++) { int x=s[i]-'a'; if(a[k].next[x]==-1) { a[++p].ini(); a[k].next[x]=p; } k=a[k].next[x]; a[k].num++; } } inline int query(char *s) { int k=0; for(int i = 0; s[i]!='\0'; i++) { int x=s[i]-'a'; if(a[k].next[x]==-1) return 0; k=a[k].next[x]; } return a[k].num; } int main() { po(); char s[11]; while(gets(s),*s) insert(s); while(gets(s)) printf("%d\n",query(s)); }
相关文章推荐
- 【算法】寻找和为定值的两个数
- Basic Git commands
- 冰冷的端午节
- 基于jquery判断浏览器版本过低代码
- Properties文件不在项目目录下的一种读取方法
- Android自动化测试(UiAutomator)简要介绍 完整版本
- ZtreeAsyncMasterFile(zTree大数据量分批次异步加载)
- jquery checkbox checked 第一次有效果 第二次以后就不好用了
- 重新打开老Eclipse工作空间后JAD插件无法正常工作
- android 签名
- Uva - 1583 - Digit Generator
- android 签名
- eclipse调试java程序的个别技巧
- Uva - 1583 - Digit Generator
- ubuntu 下 eclipse Ctrl+Alt+Down 不工作
- cocos2dx-3.0 : EventDispatcher
- jquery checkbox checked 第一次有效果 第二次以后就不好用了
- 关于__proto__和prototype的一些理解
- android 签名
- JVM常量池和八种基本数据及字符串