POJ 2418 Hardwood Species 【Trie树】
<题目链接>
题目大意:
给你一堆字符串,让你按字典序输出他们出现的频率.
解题分析:
首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串的数量,最后用dfs寻找Trie树中所有的单词,下面代码是用数组实现的Trie树。当然,本题也可用快排或者map直接水过。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = *;
const int SIZE = ; //ASCII中有128个常用字符
char str[];
int all;
struct Trie{
int ch[MAXN][SIZE];
int num[MAXN];
int pos; //sz为遍历到该节点的编号
void init(){
pos = ;
memset(ch[],,sizeof(ch[]));
}
int idx(char c){return c - ' ';} //因为本题的字符范围是所有字符,所以这里是减去字符中ASCII最小的 ' '(空格)
void insert(char *s){
int now = ,n = strlen(s);
for(int i=;i<n;i++){
int next = idx(s[i]);
if(!ch[now][next]){ //如果该节点没遍历过,那么就新建该节点
memset(ch[pos],,sizeof(ch[pos])); //注意这里是ch[pos],而不是ch[now],因为now之前可能已经由其它的子节点了
ch[now][next] = pos++; //若该节点没被遍历过,给该节点编号
num[now] = ;
}
now = ch[now][next];
}
num[now]++;
}
void dfs(int u,int t){
str[t] = '\0'; //str[]存下该单词的每一位字符,虽然一开始就给每一位都赋'\0',但是如果该位置的字符符合要求,'\0'就会被该字符覆盖,不影响结果
if(num[u])printf("%s %.4f\n",str,(double)num[u]*/all); //如果遍历到前缀是完整单词的节点,则将该单词输出
for(int i = ;i < SIZE;i ++){
if(ch[u][i]){
str[t] = i+' '; //第t位存下该字符
dfs(ch[u][i],t+);
}
}
}
}word;
int main(){
word.init();
char s[];
all = ;
while(gets(s)){
if(s[]=='\0')break;
word.insert(s);
all++ ; //记录所有单词个数
}
word.dfs(,);
return ;
}
快排+map
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int N = 1e4+;
const int M = 1e6+;
map<string,int>mp;
vector<string>vec; int main(){
string str;
int cnt=;
while(getline(cin,str)){
if(!mp[str])vec.push_back(str);
mp[str]++;
cnt++;
}
sort(vec.begin(),vec.end());
for(int i=;i<vec.size();i++){
cout<<vec[i]<<" ";
printf("%.4lf\n",(mp[vec[i]]/cnt*1.0)*1.0*);
}
}
2018-10-27
POJ 2418 Hardwood Species 【Trie树】的更多相关文章
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- POJ训练计划2418_Hardwood Species(Trie树)
解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- POJ - 2418 Hardwood Species(map,trie,BST)
1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...
- poj 2418 Hardwood Species (map)
题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- POJ 2418 Hardwood Species( AVL-Tree )
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...
随机推荐
- Confluence 6 MBeans
你可以使用下面的 Confluence MBeans 来实时查看你 Confluence 实例运行的实时信息. CacheStatistics 这个 MBean 显示了 Confluence 有关的 ...
- node.js 框架express关于报错页面的配置
1.声明报错的方法,以及相对应的页面 //把数据库的调用方法配置到请求中 server.use((req, res, next) => { //把数据库存入req中 req.db = db; / ...
- mybatis初始化过程
mybatis初始化如下: //加载配置文件InputStream resourceAsStream = Resources.getResourceAsStream("testMybatis ...
- HTML中body元素的属性
body元素的属性 属性 描述 text 设定页面文字颜色 bgcolor 设定页面背景颜色 background 设定页面背景图像 bgproperties 设定页面的背景图像为固定状态(不随页面的 ...
- Python匿名函数(lambda)
result = lambda [arg1 [, arg2, .....]]:expression result:用于调用lambda表达式 [arg1 [, arg2, -]]:可选参数,用于传递参 ...
- C++ Primer 笔记——union
1.union是一种特殊的类.一个union可以有多个数据成员,但是在任意时刻,只有一个数据成员可以有值.当我们给union的某个成员赋值之后,该union的其他成员就变成未定义的状态了.分配给一个u ...
- win(64位)环境下oracle11g的安装方法
将压缩文件解压到一个目录中,该目录结构如下: 安装步骤(摘自网络): 1.进入数据库解压目录,双击其中的“setup.exe”文件,稍等片刻出现如下“配置安全更新“界面,取消“我希望通过My Orac ...
- 微信录音文件上传到服务器以及amr转化成MP3格式,linux上转换简单方法
微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...
- windows下使用Play框架
play类似于Spring这里的web框架.特点:MVC.函数编程. 版本:play 2.1.3 一.play命令 #play ~compile 功能:持续编译.在cmd中运行这个命令,只要 ...
- Two Sum【LeetCode】
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...