HDU 1251 统计难题
来源:互联网 发布:网络打印共享器怎么用 编辑:程序博客网 时间:2024/03/28 23:55
惊奇地发现这个题字典树超内存,所以采用了map对于添加的每个单词的所有前缀都进行统计出现次数,这样效率低一点,但是不会超内存。
问题描述:
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
样例输入:
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
banana
band
bee
absolute
acm
ba
b
band
abc
样例输出:
对于每个提问,给出以该字符串为前缀的单词的数量.
2
3
1
0
AC代码:
#include <cstdio>#include <algorithm>#include <cstring>#include <map>#include <iostream>using namespace std;int main(){ char a; string x; map<string, int> preCount; while(true) { scanf("%c", &a); if(a == '\n') { scanf("%c", &a); x = ""; } if(a == '\n')//连续两个\n结束了单词表的输入 { break; } x += a; //将一个单词一个字母一个字母地读入,然后存入map preCount[x] += 1; //对应前缀次数加一 } while(cin >> x) { printf("%d\n", preCount[x]); } return 0;}
0 0
- HDU 1251 统计难题
- HDU 1251 统计难题
- hdu 1251 统计难题
- HDU 1251:统计难题
- HDU 1251 统计难题
- Hdu-1251 统计难题
- hdu 1251 统计难题
- hdu 1251 统计难题
- hdu 1251 统计难题
- HDU-1251-统计难题
- HDU 1251 统计难题
- hdu 1251统计难题
- hdu 1251统计难题
- hdu 1251 统计难题
- HDU 1251 统计难题
- hdu 1251统计难题
- HDU 1251 统计难题
- hdu 1251 统计难题
- cakephp中使用ajax获得数据,动态折线显示
- 在vs2010下编译OpenSSL
- ORA-06553:PLS-306:wrong number or types of arguments in call to ''
- 设置不自动弹出软键盘的最简洁解决方案
- VBS 改 IP(modify IP by VBS)
- HDU 1251 统计难题
- hdu 5595
- Java Notes-1
- java基础第八天——正则、集合与数组的区别
- java模拟异步消息机制
- Java Notes-2
- POJ 1062 迪杰斯特拉
- HDOJ 5595 GTW likes math(简单数学,模拟)
- Solve 0x80004005 issue, cannot access sharefolder under windows2012