这道题题目里没有给定数据范围 我开了2005 疯狂的WA
然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~
只是记录下这道题学到的方法吧:
for(rt = 0; *s; rt = nxt, ++s)
{
nxt=tree[rt][*s-tb];
if(!nxt)
{
nxt=tree[rt][*s-tb]=top;
memset(tree[top],0,sizeof(tree[top]));
top++;
}
tree[nxt][tk]++;//////////
}
//////////////处,假设改为tree[rt][tk]表示某个节点有几个孩子,tree[nxt][tk]++经过当前字母的单词个数
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int tk=26;
const int tb='a';
const int MAXN= 500000; int top,tree[MAXN][tk+2]; void init()
{
top=1;
memset(tree[0],0,sizeof(tree[0]));
} void Insert(char *s, int Rank=1)
{
int rt,nxt;
for(rt = 0; *s; rt = nxt, ++s)
{
nxt=tree[rt][*s-tb];
if(!nxt)
{
nxt=tree[rt][*s-tb]=top;
memset(tree[top],0,sizeof(tree[top]));
top++;
}
tree[nxt][tk]++;
}
} int sea(char *s)
{
int rt;
for(rt=0;rt=tree[rt][*s-tb];)
{
if(*(++s)==0)return tree[rt][tk];
}
return 0;
} char str[150],pat[150]; int main()
{
//freopen("hdu1251.txt","r",stdin);
char cc;
int ans=0;
init();
while(gets(str)&&str[0])
{
Insert(str);
}
while(~scanf("%s",pat))
{
//cout << pat << endl;
printf("%d\n",sea(pat));
} return 0;
}