链接:

https://vjudge.net/problem/HDU-2072

题意:

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

思路:

字典树, 插入的时候判断是否为重复插入即可.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e6+10; int cnt, n;
string word; struct Node
{
bool Ends;
int Next[30];
void Init()
{
Ends = false;
memset(Next, 0, sizeof(Next));
}
}Trie[MAXN]; bool Insert(string val)
{
int root = 0;
int len = val.size();
for (int i = 0;i < len;i++)
{
if (Trie[root].Next[val[i]-'a'] == 0)
{
Trie[root].Next[val[i]-'a'] = ++cnt;
Trie[cnt].Init();
}
root = Trie[root].Next[val[i]-'a'];
}
if (Trie[root].Ends)
return false;
Trie[root].Ends = true;
return true;
} int main()
{
while (getline(cin, word))
{
if (word[0] == '#')
break;
cnt = 0;
int ans = 0;
Trie[0].Init();
stringstream ss(word);
while (ss >> word)
{
if (Insert(word))
ans++;
}
printf("%d\n", ans);
for (int i = 0;i <= cnt;i++)
Trie[i].Init();
} return 0;
}

HDU-2072-单词数(字典树)的更多相关文章

  1. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

  2. hdu2072 单词数 字典树

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

  3. hdu 2072 单词数(字符串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 题意 每行输入由小写字母和空格组成,统计每行中不同的单词数. 题解 题解一 比较简洁的解法,读入 ...

  4. HDU 2072 单词数 详细解答

    题目 单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  6. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  7. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  9. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  10. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

随机推荐

  1. Django模块

    django.contrib.humanize 一系列Django的模板过滤器,有助于向数据添加“人文关怀”. 把'django.contrib.humanize'添加到INSTALLED_APPS设 ...

  2. PHP中的闭包

    1.语句结构 在PHP中,由于存在函数内部不能访问全局作用的,所以就需要一种可以引入上一级作用域的语法结构,这种就是 function () use () {} 将需要引入到这个函数作用于内的变量写入 ...

  3. Asp.net core Identity + identity server + angular + odata + 权限管理

    今天踩了一个坑. 网站发布后看到了一个 error Refused to execute inline script because it violates the following Content ...

  4. [转载]flex中的正则表达式

    原文:https://blog.csdn.net/hczhiyue/article/details/20483209 (1)单字符匹配* ‘x’ 匹配字符 x.* ‘.’ 匹配任意一个字符(字节),除 ...

  5. eclipse设置字体

  6. 抓包工具之tcpdump

    tcpdump 官网 -> http://www.tcpdump.org 1. 安装步骤 在官网分别下载 Tcpdump.Libpcap 这两个包链接 在安装Tcpdump之前,先安装Libpc ...

  7. vue学习(5)-评论功能(利用父组件的方法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. VS2015 中统计整个项目的代码行数

    在一个大工程中有很多的源文件和头文件,我如何快速统计总行数? ------解决方案--------------------b*[^:b#/]+.*$^b*[^:b#/]+.*$ ctrl + shif ...

  9. /etc/ld.so.conf.d/目录下文件的作用

    在了解/etc/ld.so.conf.d/目录下文件的作用之前,先介绍下程序运行是加载动态库的几种方法: 第一种,通过ldconfig命令     ldconfig命令的用途, 主要是在默认搜寻目录( ...

  10. apache启动错误 AH00072: make_sock: could not bind to address [::]:443 windows系统端口/进程查看

    1. netstat -ano|findstr " 2. tasklist|findstr "