HDU-2072-单词数(字典树)
链接:
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-单词数(字典树)的更多相关文章
- HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...
- hdu2072 单词数 字典树
字典树裸题 #include<stdio.h> #include<string.h> ][]; ]; int cnt; int ans; void Insert(char *w ...
- hdu 2072 单词数(字符串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 题意 每行输入由小写字母和空格组成,统计每行中不同的单词数. 题解 题解一 比较简洁的解法,读入 ...
- HDU 2072 单词数 详细解答
题目 单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
随机推荐
- [转帖]云服务器使用CentOS、Debian、Ubuntu的哪个版本
云服务器使用CentOS.Debian.Ubuntu的哪个版本 2018-09-09 12:32:45作者:ywnz稿源:云网牛站 https://ywnz.com/linuxyffq/2986.ht ...
- 【Python】【基础知识】【内置函数】【object的使用方法】
原英文帮助文档: class object Return a new featureless object. object is a base for all classes. It has the ...
- Android Application的基本组件介绍
一个Android应用通常由一个或多个基本组件组成,常用的一般有Activity.Service.BroadcastReceiver.ContentProvider.Intent等等. ⒈Activi ...
- 剑指offer9:青蛙变态跳台阶,1,2,3……,n。
1. 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 2. 思路和方法 每个台阶都有跳与不跳两种情况(除了最后一个台阶),最后 ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
- Android 直连SQL
在工作中遇到需求需要Android直接连接SQL,看了一些人说不建议直连,但我对性能没有要求,甚至说只要在局域网内能够使用就行,简单说把手机当作一个简单的移动操作点. 代码的话,网上都有比如: htt ...
- promise使用的正确方式
一开始恨不能理解下面的代码,为什么可以一直then下去,什么时候要直接return xxx,什么时候return 一个promise,什么时候用Promise.resolve() function ...
- vscode调试npm包技巧
官网文档:https://code.visualstudio.com/docs/nodejs/nodejs-debugging node调试方法(日志和debuuger):https://blog.r ...
- java_day05_类和对象
chap05目标:类和对象---------------------------------------------- 1.OOP特征概述 Java的编程语言是面向对象的,采用这种语言进行编程称为面向 ...
- AxMath_V2.5.0.0安装和激活
目录 1. 相关推荐 2. 按 3. AxMath 功能与特色 4. 软件介绍 4.1. 编辑与排版 4.2. 科学计算功能 4.3. 输出与发布 4.4. 运行环境 4.5. 破解说明 5. 安装步 ...