要注意二点 。

这组数据

1
6
she
he
he
say
shr
her
yasherhs
出现重复的,也要算。所以这里答案为4; 这一组
1
6
she
he
he
say
shr
her
yasherhe
查询单词中he出现过,所以后面的he不能记录,所以答案为4;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct trie
{
trie *next[];
int flag;
int ok;
};
trie *root;
void init()
{
root=(trie*)malloc(sizeof(trie));
for(int i=;i<;i++)
root->next[i]=NULL;
root->flag=;//标记是否有单词
root->ok=;//标记该单词是否已经被使用
}
void insert(char *str)
{
int i,j,len=strlen(str);
trie *p=root,*q;
for(i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
q=(trie*)malloc(sizeof(trie));
for(j=;j<;j++)
q->next[j]=NULL;
q->flag=;
q->ok=;
p->next[id]=q;
}
p=p->next[id];
if(i==len-)
p->flag++;
}
}
int query(char *str)
{
int i,j,len=strlen(str);
int ans=,flag; for(i=;i<len;i++)
{
trie *p=root;
flag=;
for(j=i;j<len;j++)
{
int id=str[j]-'a';
if(p->next[id]==NULL)
{
flag=;
break;
}
p=p->next[id];
if(p->flag>&&!p->ok)//flag标记是否有单词,ok标记该单词是否被使用
{
p->ok=;
ans+=p->flag;
}
}
if(!flag)
i+=j-i;
}
return ans;
}
void freetrie(trie *p)
{
for(int i=;i<;i++)
{
if(p->next[i]!=NULL)
freetrie(p->next[i]);
}
free(p);
}
char temp[];
int main()
{
int i,j,n,t;;
scanf("%d",&t);
while(t--)
{
init();
char str[]; scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",str);
insert(str);
}
scanf("%s",temp);
int ans=query(temp);
printf("%d\n",ans);
freetrie(root);//之前没释放内存超时了。。
}
}

hdu2222 字典树的更多相关文章

  1. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  5. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  6. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  7. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  8. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  9. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. ZOJ 3829 Known Notation --贪心+找规律

    题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...

  2. 最长回文子串Manacher算法模板

    Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 首先,在字符串s中,用rad[i]表示第i个字符 ...

  3. MySQL数据库学习笔记(三)----基本的SQL语句

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Unity CombineChildren和MeshCombineUtility

    原理 Unity3D如何通过CombineChildren和MeshCombineUtility优化场景? 首先解释下联结的原理和意思:文档里说,显卡对于一个含100个面片的物体的和含1500个面片的 ...

  5. 使用appscan实现多站扫描简单自动化

    因为appscan在新建扫描任务的时候只能输入一个target,并且没有awvs/nessus那样提供web接口,导致我以前一直以为appscan不能像awvs那样批量建立任务自动扫描. 不过,今天要 ...

  6. [2]Telerik Extensions for ASP.NET MVC 中文教程(2)

    上一篇文章对Telerik MVC Extensions作了一个大概的介绍,这篇文章将介绍如何将Telerik MVC Extensions添加到项目中.有以下两种方式可以将Telerik MVC E ...

  7. ruby 元编程

    一 对象模型 kernel Module Kernel.private_instance_methods.grep(/^pr/)   private method 1 如果一个方法接收者不是你自己,一 ...

  8. Wireshark命令行工具tshark

    Wireshark命令行工具tshark 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把Data部 ...

  9. MySQL数据备份小结

    一 MySQL备份恢复总结: 1,备份所有库 2,分库备份 3,备份某库中的某表 4,备份某库中的多个表 5,分表备份 6,只备份表结构 7,只备份数据 二 MySQL备份恢复参数总结: -A 备份所 ...

  10. Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...