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> ...
随机推荐
- 前端图片缓存之通过img标签加载GIF只能播放一次问题(转载)
最近项目中要求再网页中插入一张gif图片,让用户每次到达该位置时动一次,所以我们就制作了一张只动一次的gif图片通过img标签引入.当用户进入该位置时,通过remove()清除图片然后重新append ...
- ionic3 title 不居中问题
<ion-navbar> <ion-title style="text-align: center;">{{naveTitle}}<button io ...
- ES6笔记
/** * Created by Administrator on 2017/4/13. */ /*---------------------Es6编码规范---------------------* ...
- LeetCode(67):二进制求和
Easy! 题目描述: 给定两个二进制字符串,返回它们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = " ...
- Unity3D用户手册
Unity Manual 用户手册 Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best int ...
- cf1144E 假高精度平均数
/* 先一轮求和,再一轮做除法 */ #include<bits/stdc++.h> using namespace std; ],s2[]; ],n; int main(){ cin&g ...
- 微信公众号开发调用自带地图 不显示(openLocation)
1.需要在wx.config中声明需要使用的功能(openLocation) 例如: wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端aler ...
- 提取Word里的文本内容 C#
using DocumentFormat.OpenXml.Packaging; public static string TextFromWord(string path) { const strin ...
- 页面注册系统--使用forms表单结合ajax
页面注册系统--使用forms表单结合ajax 在Django中通过forms构建一个表单 1.urls.py 配置路由 from django.conf.urls import url from d ...
- DDD实践:领域事件
要求:修改good表,添加 organization 基础定义 用于引发和调度事件的延迟方法 AddDomainEvent Domain\SeedWork\Entity.cs public abstr ...