题目:hdu 1251
题意:给定一堆单词,再给定一些前缀用于查询,每次输入一个前缀,就输出单词中以这为前缀的单词数
题解:字典树基础
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
char s[15];
struct Trie//字典树
{
int cont;
Trie *c[28];
Trie()
{cont=0;for(int i=0;i<=26;i++)c[i]=NULL;}
};
Trie *root;
void insert_into(char *str)//加入字典
{
Trie *tmp=root;
int len=strlen(str);
for(int i=0;i<len;i++)
{
int pos=str[i]-'a';
if(tmp->c[pos]==NULL)
{
tmp->c[pos]=new Trie();
}
tmp=tmp->c[pos];
tmp->cont++;
}
}
int findTrie(char *str)//查找
{
Trie *tmp=root;
int len=strlen(str);
for(int i=0;i<len;i++)
{
int