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类型的变 ...
随机推荐
- QT Creator调用动态链接库实例
#include<iostream> #include <QLibrary> using namespace std; int main() { cout<<&qu ...
- 9.22 window对象、document对象
一.window对象: 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null,可以利用这个属性来关闭源窗口 dialogArgume ...
- GridView中使用分页控件
前台:导入<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix= ...
- Mac 安装 eclipse
总是看着安卓的代码感觉手痒痒,闲来无事在 Mac 端安装了一下eclipse 提高逼格. 1.官网(http://www.eclipse.org/downloads/)下载所需的安装包,具体步骤如图: ...
- HTML标记语法之列表元素
1.无序列表 <ul> <li type=”项目符号类型”></li> <li type=”项目符号类型”></li> <li typ ...
- error TRK0002
运行程序出现error TRK0002的原因是因为3ds max中打开了程序生成的模型,同时使用导致memory conflict,然后随之出现一些乱七八糟的问题. 只要将3ds max重置即可,即不 ...
- 字典树(codevs 4189)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 最经,skyzhong得到了 ...
- 解决VS2010 C++ DLL不能断点调试的问题
问题产生的过程是这样的,向exe项目(CSharp)中添加dll工程(c++开发)的引用,并将引用工程的属性“Link Library Dependencies”的值设为true,这样,在不加入lib ...
- Gif图片制作
gif图片是博客中展示项目效果的一种很好的方式,为我们的app制作一张gif图片并不复杂,录制屏幕采用系统自带的QuickTime Player,制作gif采用PicGIF软件.licecap软件更是 ...
- Redis笔记(七)Java实现Redis消息队列
这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...