题目链接:http://poj.org/problem?id=2418

题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出

思路:最简单的就是用map来写,关于字典树的解法,因为字典序的先序遍历是排序的,所以只需建好树后先序遍历一下树就可以满足题目要求的输出方式了。

坑点:树名会出现空格,而且题目也没说明可能出现的字符集合,所以树的孩子结点要有128个。 G++无限WA换C++就能AC,这个无解。。。

map:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <time.h>
using namespace std;
typedef long long int LL;
char str[];
map<string, int>word;
int main(){
int n = ;
while (gets(str) != NULL){
n++; string ss = string(str);
word[ss]++;
}
for (map<string, int>::iterator it = word.begin(); it != word.end(); it++){
cout << (it->first);
printf(" %.4lf\n", (it->second)*1.0 / n*100.0);
}
return ;
}

字典树:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <time.h>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
char str[];
struct Trie{
int val;
int child[];
Trie(){
val = ;
memset(child, , sizeof(child));
}
}trie[MAXN];
int trieN, n;
void Insert(char *str){
int d, x = ;
for (int i = ; str[i]; i++){
d = str[i];
if (trie[x].child[d] == ){
trie[x].child[d] = ++trieN;
}
x = trie[x].child[d];
}
trie[x].val++;
}
void Search(int u, int len, char *str){
for (int i = ; i < ; i++){
if (trie[u].child[i]){
str[len] = i;
Search(trie[u].child[i], len + , str);
}
}
if (trie[u].val){
str[len] = '\0';
printf("%s %.4lf\n", str, (trie[u].val*1.0 / n)*100.0);
}
}
int main(){
trieN = , n = ;
while (gets(str) != NULL){
n++; Insert(str);
}
Search(, , str);
return ;
}

POJ 2418 字典树的更多相关文章

  1. POJ 2503 字典树

    题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2]  意思是string2的值为string1. 然后给定一波 ...

  2. poj 3764 字典树

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7332   Accepted: 1 ...

  3. POJ 2001 字典树(入门题)

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

  4. POJ 2513 字典树+并查集+欧拉路径

    Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...

  5. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  6. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  7. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  8. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  9. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

随机推荐

  1. HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...

  2. mac系统下如何解压.car文件

    纯手打: 1.去github下载demo然后运行  github地址:https://github.com/steventroughtonsmith/cartool 2.找到项目下cartool的位置 ...

  3. Jquery获取select 控件的change事件时选中的值

    HTML代码如下: <div class="col-sm-9 col-xs-12"> <select id="groupid" class=& ...

  4. 怎么查询电脑ip地址

    方法一:本地连接查看法 方法二:命令行法 摘自:http://jingyan.baidu.com/article/870c6fc3d509a1b03fe4be06.html

  5. July 6th, Week 28th Wednesday, 2016

    Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...

  6. 解决eclipse中maven web工程打包成war(发布到tomcar)时lib中没有jar包的解决方法

    可能有两个原因:1.maven中某些jar包下载不下来 从其他地方下载jar文件放到相应maven本地库的.m2里2..classpath文件中缺少(下面代码的作用是制定maven的jar发布路径)& ...

  7. BeatifulSoup文档地址

    http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

  8. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  9. Maven构建Hadoop Maven构建Hadoop工程

    一.安装maven linux eclipse3.6.1 maven安装 二:官网依赖库 我们可以直接去官网查找我们需要的依赖包的配置pom,然后加到项目中. 官网地址:http://mvnrepos ...

  10. cascade 介绍与用法 ( oracle)

    级联删除,比如你删除某个表的时候后面加这个关键字,会在删除这个表的同时删除和该表有关系的其他对象 1.级联删除表中的信息,当表A中的字段引用了表B中的字段时,一旦删除B中该字段的信息,表A的信息也自动 ...