<题目链接>

题目大意:

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

解题分析:

首先,这是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. iOS 上线过程中出现的问题总结

    1:  因为使用后台播放音乐,在 info 里面添加 required background modes  的 key, 如果你的代码里面没有后套播放音频的功能, 不要添加 required back ...

  2. Oracle存储过程中跳出循环的写法

    注:本文来源于: <  Oracle存储过程中跳出循环的写法   > Oracle存储过程中跳出循环的写法 记录exit和return的用法 1:exit用来跳出循环 loop IF V_ ...

  3. Confluence 6 下载和安装 Oracle thin 驱动

    基于许可证的考虑,我们不能将 Oracle 的驱动捆绑到 Confluence 中.如果你希望你的 Confluence 能够连接到 Oracle 数据库,你需要: 停止 Confluence. 进入 ...

  4. nginx常用命令及简单配置

    nginx常用命令 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx(windows下start nginx); nginx -s quit 停止ng ...

  5. Nginx的进程模型及高可用方案(OpenResty)

    1. Nginx 进程模型简介 Nginx默认采用多进程工作方式,Nginx启动后,会运行一个master进程和多个worker进程.其中master充当整个进程组与用户的交互接口,同时对进程进行监护 ...

  6. Wireless Penetration Testing(命令总结)

    1.对本书出现的无线网络涉及的命令做一总结 查看无线网卡( Create a monitor mode interface using your card as shown in the follow ...

  7. Java基本语法(一)

    一.标识符 (1)标识符就是在编写程序时给类,变量,方法等起的名字 (2)标识符的命名规则:标识符由字母,数字,下划线和$组成;第一个字符不能是数字;不能与关键字重名 二.关键字 定义:也称保留字,是 ...

  8. 在windows下Apache安装配置

    安装,从官网下载,安装即可.   配置遇到一些问题: 1.  the requested operation has failed 这是因为安装后的文件目录没有没有写的权限.通过安全设置安装目录的所有 ...

  9. 使用CSS选择器定位页面元素

    摘录:http://blog.csdn.net/defectfinder/article/details/51734690 CSS选择器也是一个非常好用的定位元素的方法,甚至比Xpath强大.在自动化 ...

  10. IDEA文件对比