裸前缀树 只不过 指针和动态内存 我比较陌生 以前没用过啊QAQ
熟悉语法 水一日志 告诉自己 就算noip洗白了 也不要放弃
#include<cstdio>
#include<cstring>
#include<malloc.h>
#define MAXN (30)
#define cle(a,b) memset(a,b,sizeof(a))
#define loop(i,j,k,l) for(int i=j;l>0?i<=k:i>=k;i+=l)
struct trie {
trie *next[MAXN];
int v;
}*root;
char s[MAXN];int len;
int id;
void create() {
trie *p=root,*q;
loop(i,0,len,1) {
id=s[i]-'a'+1;
if(p->next[id]==NULL) {
q=(trie*)malloc(sizeof(trie));
loop(j,1,26,1) {
q->next[j]=NULL;
}
q->v=1;
p->next[id]=q;
}
else{
p->next[id]->v++;
}
p=p->next[id];
}
}
void readdata() {
gets(s);
len=strlen(s)-1;
while(s[0]!=0) {
create();
cle(s,0);
gets(s);
len=strlen(s)-1;
}
}
int find() {
trie *p=root;
loop(i,0,len,1) {
id=s[i]-'a'+1;
if(p->next[id]==NULL){
return 0;
}
p=p->next[id];
if(i==len) {
return p->v;
}
}
}
void solve() {
while(scanf("%s",s)!=EOF) {
len=strlen(s)-1;
printf("%d\n",find());
cle(s,0);
}
}
int main() {
root=(trie*)malloc(sizeof(trie));
loop(i,1,26,1) {
root->next[i]=NULL;
}
readdata();
solve();
}