题目没给范围,动态建树做的~
#include <stdio.h>
#include <string.h>
struct Trie
{
Trie *ch[26];
int sz;
}p;
void init()
{
for(int i = 0; i < 26; i++)
{
p.ch[i] = NULL;
}
p.sz = 0;
}
int idx(char c)
{
return c - 'a';
}
void insert(char *ss)
{
Trie *H;
int len = strlen(ss);
H = &p;
for(int i = 0; i < len; ++i)
{
int c = idx(ss[i]);
if(H->ch[c] == NULL)
{
//printf("new:%c\n", c+'a');
H->ch[c] = new Trie;
H->ch[c]->sz = 0;
for(int j = 0; j < 26; j++)
{
H->ch[c]->ch[j] = NULL;
}
}
H->ch[c]->sz++;
H = H->ch[c];
}
}
int query(char *ss)
{
Trie *H = &p;
int len = strlen(ss);
for(int i = 0; i < len; i++)
{
int c = idx(ss[i]);
if(H->ch[c] == NULL)
{
return 0;
}
H = H->ch[c];
}
return H->sz;
}
int main()
{
char ch[100000];
init();
while(gets(ch), ch[0] != '\0')
{
insert(ch);
}
while(gets(ch))
{
//puts(ch);
printf("%d\n", query(ch));
}
return 0;
}