hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 25233 Accepted Submission(s):
10347
注意:本题只有一组测试数据,处理到文件结束.
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string> using namespace std; char str[];
string ss;
char ch1[],ch[]; int main()
{
map<string,int>qq;
//qq[string]=int;
while (gets(str))
{
int len=strlen(str);
if (len==)
break;
int j;
for (int i=;i<len;i++)
{
for (j=;j<=i;j++)
{
ch1[j]=str[j];
}
ch1[j]='\0';
ss.assign(ch1);
qq[ss]++;
}
}
while (~scanf("%s",&ch))
{
//cout<<1111111<<endl;
cout<<qq[ch]<<endl;
}
return ;
}
第二种,字典树。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
int Count;
node *next[];
node() //初始化数据
{
for (int i=;i<;i++)
next[i]=NULL;
Count=;
}
}; node *p,*root=new node();
void Insert(char *s)//插入新单词,即建立字典树
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)
p->next[k]=new node();//判断是不是新节点,如果是分配创建一个新节点来存贮 ,即root的next域对应的k位置是否为空
p=p->next[k];
p->Count++; //记录此字母出现的次数
}
} int Search(char *s)
{
int i,k;
p=root;
for (i=; s[i]!='\0'; i++)
{
k=s[i]-'a';
if (p->next[k]==NULL)//一旦查找不到,立即跳出
return ;
p=p->next[k];
}
/*if (s[i]!='\0')//s[i]!=0表示中间
return 0;*/
return p->Count;
} int main()
{
char s[];
while (gets(s))
{
int len=strlen(s);
if (len==)
break;
Insert(s);
}
while (gets(s))
{
printf ("%d\n",Search(s));
}
return ;
}
hdu 1251 统计难题(字典树)的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- hdoj 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...
随机推荐
- java异常处理的throw和throws的区别
1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...
- MySql的多存储引擎架构, 默认的引擎InnoDB与 MYISAM的区别(滴滴)
1.存储引擎是什么? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术, ...
- QComboBox 树形视图选择
QComboBox 控件支持树形图显示. A. void QComboBox::setModel(QAbstractItemModel *model): B. void QComboBox::se ...
- python beautifulsoup/xpath/re详解
自己在看python处理数据的方法,发现一篇介绍比较详细的文章 转自:http://blog.csdn.net/lingojames/article/details/72835972 20170531 ...
- 【刷题】BZOJ 3669 [Noi2014]魔法森林
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- hdu2138 Miller_Rabin
Description Give you a lot of positive integers, just to find out how many prime numbers there are ...
- Java考试题之四
QUESTION 73 Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14 ...
- NOIWC前的交流题目汇总
RT 2018.12.27 i207M:BZOJ 4695 最假女选手 以维护最大值为例,记录最大值和严格次大值和最大值的出现次数,然后取min的时候递归到小于最大值但大于次大值修改,这个就是最重要的 ...
- windows10下R配置Rstdio,怎么处理
首先要确保电脑上只有一个R程序,然后官网下载Rstdio安装包. 配置:选择Rstdio配置界面的第三项,然后关联到R的安装文件夹下的BIN文件夹即可. 但是,直接打开Rstdio的话,界面会一片空白 ...
- Eloquent 条件查询——tucker-eric/eloquentfilter 笔记
请阅读 https://github.com/Tucker-Eric/EloquentFilter , 里面有很全的文档和注释,以下仅列出关键部分. 1. 安装 composer require tu ...