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类型的变 ...
随机推荐
- CString之GetBuffer与ReleaseBuffer
我们知道,CString是MFC中提供的方便字符串操作的一个类,非常好使,具有自动动态内存管理功能. GetBuffer()主要作用是将字符串的缓冲区长度锁定: ReleaseBuffer()则是解除 ...
- poj 1102.LC-Display 解题报告
题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...
- ORACLE 远程导入导出数据库
Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. ...
- DX使用texconv工具批处理dds格式图片
texconv D:\png\*.* -o E:\dds -m 5 -f dxt3 -ft dds 上述命令的意思是把D:\png目录下的全部文件(当然可以指定特定格式例如*.png)转换成dds格式 ...
- Redis自定义动态字符串(sds)模块(一)
Redis开发者在开发过程中没有使用系统的原始字符串,而是使用了自定义的sds字符串,这个模块的编写是在文件:sds.h和sds.c文件中.Redis自定义的这个字符串好像也不是很复杂,远不像ngin ...
- Scanner和BufferedReader
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- python基础——使用@property
python基础——使用@property 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9 ...
- Spring 注释 @Autowired 和@Resource
一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired属于Spring的:@Resource为JSR-250标准的注释,属于J ...
- 用java来删除数组中指定的元素
public static void main(String[] args){ String[] a = new String[]{"1","5" ...
- Java Eclipse进行断点调试
如何调试Java程序? 大家最开始学习Java,都会觉得IDE调试好高端有木有,其实很简单了. 下文会尽量简单直观的教会你在Eclipse中调试,其他的IDE调试步骤也是类似的. 1.在你觉得有错的地 ...