要注意二点 。

这组数据

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. 不会UML的程序员不是好构架师?

    情况描述 我已经工作两年半, 参加过一个网页游戏项目和一个IOS应用项目, 自以为参与度非常高, 也经常涉及到底层引擎和主逻辑业务. 目前想更快的向构架师方向发展. 最近在看\<Learning ...

  2. codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)

    题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...

  3. Matlab手册翻译

    MATLAB 语言是一种工程语言,语法很像 VB 和 C,比 R 语言容易学,你知道 R 语言的语法有多糟糕么.同样,相对于 Python,MATLAB 的优点是天生为了算而算,数与数之间的运算就是矩 ...

  4. Bluestacks 使用

    Bluestack介绍 一款能够在PC模拟Android的好用模拟器 官网:http://www.bluestacks.cn/ 早在2012年就使用上它了,但一直未发现他的许多优点,在使用过其它的两款 ...

  5. java22 - 1 多线程之 单线程和多线程的图解

  6. xcode插件XAlign

    一款十分强大的自定义对齐模式插件 开源地址:https://github.com/qfish/XAlign/ 终端输入命令: $ curl http://qfi.sh/XAlign/build/ins ...

  7. 16Spring_AOP编程(AspectJ)_最终通知

  8. 【转】MySQL 性能优化的最佳20多条经验分享

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.   当我们去设计数据库表结构,对操 ...

  9. 最近火到不行的微信小程序的常识

    满网都是微信小程序,技术dog们不关注都不行了.先别忙着去学怎么开发小程序,先纠正一下你对微信小程序的三观吧~~~~ 小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想 ...

  10. RAS RC4 AES 加密 MD5

    这两者唯一的相同点是设计者中都包含了MIT的Ron Revist教授.RSA是公钥密码算法,优点:不用事先通过秘密信道传递密钥,可以用于数字签名.缺点:速度慢RC4是序列密码算法,优点:速度快,缺点: ...