您的位置:首页 > 其它

hdu 1251 字典树

2015-06-08 14:55 197 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1251

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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: