要注意二点 。

这组数据

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. RabbitMQ 一二事(3) - 订阅模式(微信公众号模式)的应用

    之前讲的消费者互相可以把队列中的消息全部读取,但是不是读完整的所有信息 那么采用订阅模式就行,这就是微信公众号的模式, 比如10个人订阅了我的公众号"BeJavaGod",当我发送 ...

  2. Apache Commons 常用工具类整理

    其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包 public class ApacheCommonsT ...

  3. 三维世界的Gizmos

    Unity和Maya 今天在美术同事那儿了解些Maya常识,加上自己在Unity3D中的一点儿小操作,记录一下Gizmos 之前就知道Maya和Unity3D的轴向是一致的,在同事那儿看他操作Maya ...

  4. linux下批量替换文件内容

    1.网络上现成的资料 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` linux sed 批量替换多个文件中的字符串: (此命令很强大) ...

  5. Java AtomicInteger

    AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...

  6. Xcode中文乱码问题

    老师给拷贝的程序用Xcode打开中文显示是乱码,而预览里面是正常显示的,Xcode默认编码UTF-8没错的,怎么办呢? 解决办法:用自带的文本编辑器打开,全选,复制,Xcode中打开文件,粘贴,ok~ ...

  7. codevs 1133 表达式的值

    1133 表达式的值 2011年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descript ...

  8. variable 'xxx' unsafe in 'case'的处理

    问题描述: case get(?Player_LoopTaskInfo) of {TargetCnt, TaskStar, TaskExp} -> ok; _ -> throw(" ...

  9. 区块链技术(一):Truffle开发入门

    以太坊是区块链开发领域最好的编程平台,而truffle是以太坊(Ethereum)最受欢迎的一个开发框架,这是我们第一篇区块链技术文章介绍truffle的原因,实战是最重要的事情,这篇文章不讲原理,只 ...

  10. C语言 百炼成钢9

    //题目25:求1+2!+3!+...+20!的和 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib ...