统计难题 HDU1251
简单方法:
#include<bits/stdc++.h>
using namespace std; int main()
{
char s[];
map<string,int>ma;
while(gets(s)&&strlen(s)!=)
{
for(int i=strlen(s)-;i>=;i--)
{ ma[s]++;
s[i]='\0';
// cout<<s<<endl;
} }
while(gets(s))
{
printf("%d\n",ma[s]); } }
但是当数据过多的时候map就不行了
要用新的数据结构——字典树
有两种实现的方法 一种是指针 一种是数组
数组的实现方法更为方便 高明 快速
注意 数组一定要开到一百万 不然wa !!!
#include<bits/stdc++.h>
using namespace std;
int trie[][]={};
int sum[]={};
int pos=; void insert1( char *word )
{
int root=;
for(int i=;i<strlen(word);i++)
{ int ch=word[i]-'a';
if(trie[root][ ch ]==)
trie[root][ ch ]=++pos;
root=trie[root][ch];
sum[root]++;
} } int find1(char *word)
{
int root=;
for(int i=;i<strlen(word);i++)
{
int ch=word[i]-'a';
if(trie[root][ ch ]==)return ;
root=trie[root][ch]; }
return sum[root]; } int main()
{
char word[];
while(gets(word))
{
if(strlen(word)==)break; insert1(word); }
while(gets(word))
{
printf("%d\n",find1(word));
} }
统计难题 HDU1251的更多相关文章
- 统计难题---hdu1251字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 node *head=(node*)malloc(sizeof(node)); for(int ...
- (字典树模板)统计难题--hdu--1251
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找 代码: #i ...
- hdu1251 统计难题
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251统计难题(水字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu1251(统计难题)
这题就是一个字典树的模板题 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Othe ...
- hdu1251统计难题(trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU-1251 统计难题,字典树或者map!
统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
随机推荐
- SQL 语言类型
结构化查询语言(Structured Query Language),简称SQL,是数据库编程的核心语言. SQL的发展是从1974年开始的,其发展过程如下: 1974年 - 由Boyce和Chamb ...
- IDA Pro使用技巧
DA Pro基本简介 IDA加载完程序后,3个立即可见的窗口分别为IDA-View,Named,和消息输出窗口(output Window). IDA图形视图会有执行流,Yes箭头默认为绿色,No箭头 ...
- sql 中多表查询-leetcode : Combine Two Tables
因为对数据库的内容早都忘得差不多了,所以我的第一感觉是: select Person.FirstName, Person.LastName, Address.City from Person, Add ...
- 完整版ffmpeg使用情况
protected void Page_Load(object sender, EventArgs e) { string filePath = @"D:/Prjects/MT147/exa ...
- Netty入门(3) - ChannelHandler
ChannelPipeline ChannelHandler实例的列表,用于处理或者截获通道的接收和发送数据,让用户可以在ChannelPipeline中完全控制一个事件以及处理ChannelHand ...
- js 执行上下文理解
前端基础进阶(三):变量对象详解http://www.jianshu.com/p/330b1505e41d 1.创建阶段 a.生成变量对象 1.创建arguments对象 2.functio ...
- block循环引用
block里边会有循环引用的风险,它可能对外部一个变量出现强引用,所以需要判断里边是否有循环引用,通过dealloc方法(销毁当前控制器.或销毁要测试的变量),判断是否循环引用.主要在block 里边 ...
- Informatic学习总结_day03_update组件学习
- Python startswith() 函数 判断字符串开头
Python startswith() 函数 判断字符串开头 函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str ...
- k-means 图像分割
经典的无监督聚类算法,不多说,上代码. import numpy as np import pandas as pd import copy import matplotlib.pyplot as p ...