<题目链接>

题目大意:

给你一堆字符串,让你按字典序输出他们出现的频率.

解题分析:

首先,这是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树】的更多相关文章

  1. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  2. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  3. POJ训练计划2418_Hardwood Species(Trie树)

    解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  4. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

  5. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  6. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...

  7. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  8. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  9. POJ 2418 Hardwood Species( AVL-Tree )

    #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...

随机推荐

  1. Hibernatede 一对多映射配置

    Hibernatede 一对多映射配置 以公司和员工为例:公司是一,员工是多   第一步 创建两个实体类,公司和员工        写核心配置文件hibernate.cfg.xml        写映 ...

  2. (六)STL仿函数functor

    1.仿函数为算法服务,特点是重载操作符() 2.一共分为3大类,包括算术类,逻辑运算类,相对关系(比较大小):都继承了binary_function 3.仿函数的一些调用示例,其中右边的仿函数没有继承 ...

  3. Ubuntu下Mongodb和Robo3T的安装与使用

    Mongodb的安装:https://blog.csdn.net/Canhui_WANG/article/details/78995388 Robo3T的安装:https://www.jianshu. ...

  4. Python基础之常用模块

    一.time模块 1.时间表达形式: 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: 1.1.时间戳(timestamp) :通常来说,时间 ...

  5. C++ Primer 笔记——lambda表达式

    1.一个lambda表达式表示一个可调用的代码单元,可以理解为一个未命名的内联函数,但是与函数不同,lambda表达式可能定义在函数内部.其形式如下: [capture list] (paramete ...

  6. ReactNative——页面跳转

    效果图: 进入工作目录,运行 react-native init NavigatorProject 创建项目NavigatorProject import React, { Component } f ...

  7. 用SQL语句查询zabbix的监控数据

    参考地址:http://blog.51cto.com/sfzhang88/1558254 -- 获取主机id -- 10084 select hostid from hosts where host= ...

  8. Oracle数据库表索引失效,解决办法:修改Oracle数据库优化器模式

    ALTER SYSTEM SET OPTIMIZER_MODE=RULE scope=both; 其他可以选择的模式还有ALL_ROWS/CHOOSE/FIRST_ROWS/ALL_ROWS. 应用系 ...

  9. GIT 数据结构

    Git doesn’t think of or store its data this way. Instead, Git thinks of its data more like a series ...

  10. CentOS6— Redis安装(转和延续)

    Part I. Redis安装(转载部分) 一.安装(官网地址 http://redis.io/download) wget http://download.redis.io/redis-stable ...