这题印象深刻,我刚接触acm时,以为这题是水题(因为是中文,又短),一直没做出。现再想想也是。可能也是我以前字符串掌握不好;

这题其实也可以用stl里的map写。这里我用字典树写的。其实这题算简单题了吧。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct trie
{
trie *next[];
int flag;//flag标记这里是否一个单词结束,也就是说到这里是否有一个单词;
};
trie *root;
void init()
{
int i;
root=(trie*)malloc(sizeof(trie));
for(i=;i<;i++)
{
root->next[i]=NULL;
}
root->flag=;
}
void insert(char *str)
{
trie *p=root,*q;
int i,j,len=strlen(str);
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=;
p->next[id]=q;
}
p=p->next[id];
if(i==len-)
p->flag=;
}
}
int query(char *str)
{
int i,len=strlen(str);
trie *p=root;
for(i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
return ;
else
{
p=p->next[id];
}
}
if(p->flag==)
return ;
return ;
}
int main()
{
int i,j,ans;
char str[],s[];
while(gets(str))
{
ans=;
init();
if(str[]=='#')break;
int len=strlen(str);
int num;
for(i=;i<len;i++)
{
num=;
for(j=i;j<len;j++)
{
if(str[j]==' ')
break;
s[num++]=str[j];//读取单词
}
i=j;
s[num]='\0';
//printf("%s ",s);
if(strcmp(s,"")!=&&query(s))//前面strcmp主要为了防止空格
{
insert(s);
ans++;
}
}
printf("%d\n",ans);
}
}

hdu2072 字典树的更多相关文章

  1. hdu-2072(字典树)

    字典树模板题 代码 #include<iostream> #include<algorithm> #include<cstdio> #include<cstr ...

  2. hdu2072 单词数 字典树

    字典树裸题 #include<stdio.h> #include<string.h> ][]; ]; int cnt; int ans; void Insert(char *w ...

  3. HDU-2072-单词数(字典树)

    链接: https://vjudge.net/problem/HDU-2072 题意: lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面 ...

  4. Tire树(字典树)

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【MVC 4】6.SportsSore:导航

     作者:[美]Adam Freeman      来源:<精通ASP.NET MVC 4> 前面的文章[MVC 4]5.SportsSore —— 一个真实的应用程序 建立了 Sports ...

  2. DataGridView 行、列的隐藏和删除

    ) 行.列的隐藏 [VB.NET] ' DataGridView1的第一列隐藏 DataGridView1.Columns(0).Visible = False ' DataGridView1的第一行 ...

  3. System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.

    发生未处理的域异常! System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net ...

  4. linux如何挂载windows下的共享文件

    说明:windows下有一共享文件夹APP,windows本地ip是192.168.9.155现在需要在linux服务器上挂载这个APP文件夹,linux服务器ip是192.168.9.200 操作记 ...

  5. createElement创建标签及appendChild添加到元素的后面

    var p = document.createElement('p'); var box = document.getElementsByTagName('div')[0]; box.appendCh ...

  6. 深入理解abstract class和interface(转)

    原文地址 深入理解abstract class和interface java提高篇(四)-----抽象类与接口

  7. HttpClient, 使用C#操作Web

    我们知道, .Net类库里提供了HttpWebRequest等类,方便我们编程与Web服务器进行交互. 但是实际使用中我们经常会遇到以下需求,基础类里没有直接提供相应的功能(WebClient类包含这 ...

  8. JS案例之1——pager 分页

    学习JS大半年之久,第一次自己尝试写一些小插件,写法参考网上某位牛人写代码的思路. 此处代码写的是静态分页.如果需动态分页,还可以修改下.第一次写,还有很多地方可以优化.希望各位大牛踊跃拍砖. 预览图 ...

  9. [CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数

    13.9 Write an aligned malloc and free function that supports allocating memory such that the memory ...

  10. RESideMenu左右半侧滑的功能实现,主视图会和状态栏(StatusBar)不会随着一起滑动

    具体demo去github下载,这里不详细描述