There is no data range in this question. I opened a 2005 crazy wa.
Then I opened 50000. I thought my template was wrong and I didn't have a meal in a day. I was so hungry that I still wanted to drop question a and eat it again. Who knows this is the problem ,,, haha ~~~
But record the methods learned for this question:
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]++;////////// }
////////////. If you change to tree [RT] [TK], it indicates that a node has several children, tree [NXT] [TK] ++ Number of words with the current letter
#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;}