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最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
随机推荐
- $listeners 在vue中的使用 --初学
事件回传之 $listeners 组件由下向上回传事件 <!doctype html><html lang="en"> <head> <m ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Day1 模拟赛 题解
T1:首先你要发现,对于任意一个奇数i,i xor (i-1)=1; 那么我们可以将答案转化为统计有多少个1相互异或起来: 所以答案就那么几种: 如果你用的数位DP,只能说明你太高估day1T1了: ...
- SpringCloud组件及功能介绍
1.什么是SpringClould? SpringCloud是一个基于SpringBoot实现的微服务架构开发工具.它为微服务架构中涉及的配置管理.服务治理.断路器.智能路由.微代理.控制总线. ...
- 基于全备份+binlog方式恢复数据
基于全备份+binlog方式恢复数据 将bkxt从库的全备份在rescs5上恢复一份,用cmdb操作 恢复全备后执行如下操作 set global read_only=OFF; stop slave; ...
- DVWA reCAPTCHA key: Missing
修改dvwa文件夹下文件config.inc.php change: $_DVWA[ 'recaptcha_public_key' ] = ' '; $_DVWA[ 'recaptcha_privat ...
- Sharepoint 开启App 配置App
如果没有Enable app,打开app store的时候出出现错误: Sorry, apps are turned off. If you know who runs the server, tel ...
- 题解 P2879 【[USACO07JAN]区间统计Tallest Cow】
题目链接: https://www.luogu.org/problemnew/show/P2879 思路: 先不管最大高度,我们读入一对x,y.说明,x+1~y-1之间牛的身高都小于x,y. 然后不妨 ...
- vue-cli 3.0不能用module.exports为undefined,导致第三方js都没有办法引用
新的 vue-cli 默认禁止了 commonjs 语法可以添加 babel 插件解决 yarn add @babel/plugin-transform-modules-commonjs -D 或 n ...
- 初识 vue------简单介绍
/** vue 作者:尤雨溪 类型:MVVM 准确的来说是MV框架 为什么要学习vue 1.传统的项目是通过操作dom元素来修 ...