POJ 2418 字典树
题目链接: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 字典树的更多相关文章
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- poj 3764 字典树
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7332 Accepted: 1 ...
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
随机推荐
- BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...
- js中sort()方法的用法,参数以及排序原理
sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 注:如果调用该方法时没有使用参数,将按字母顺序对 ...
- Android Volley入门到精通:定制自己的Request
经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...
- Jquery 点击按钮将其背景图换成另一张,再次点击恢复默认图片
这是Jquery代码: $(function () { $("#h1").toggle(function () { $("#h1").css("bac ...
- 整合Struts2、Hibernate、Spring
将项目中的对象和对象之间的管理,纳入spring容器,由spring管理 1 实现spring+hibernate集成 1.1 新建web项目 建立项目的包结构(package) 1.2加入jar包 ...
- Redis 数据持久化(一)
Redis的模块化开发设计的还是相当不错的,在Bio.h和Bio.c文件中定义了一个多线程的文件任务处理模块,在添加和处理任务的时候使用互斥锁和条件变量进行的同步,而且本身也支持多线程,这个模块的支持 ...
- python基础——类和实例
python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...
- 1.1 STL 概述
综述 STL = Standard Template Library,标准模板库,惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R M ...
- jQuery中读取json文件示例代码
json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取,具体示例代码如下,感兴趣的朋友可以参考下哈,希望可以帮助到你 json文件是一种轻量级的数据交互格式.一 ...
- MyEclipse破解(MEGen.java)
步骤: 1.将MEGen.java粘贴到任意web项目下,运行结果如下: 2.输入注册名:如sun,得到注册码: 3.Window >> Preference >> S ...