传送门 (<---可以点击的~)

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

描述

有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里面找出某个后缀的单词个数。

输入

输入有多组数据。
每组第一行输入n,m,0<=n,m<=100000,
第二行到n+1行:输入单词,每个单词仅有小写英文字母组成,长度不超过10。
第n+2行到n+m+1行,输入要找的单词后缀。

输出

在n个单词里面找出输入单词后缀的单词个数,然后输出。每个数据与数据之间换行。

样例输入

6 3
someone
everyone
outside
inside
somebody
nobody
one
side
body

样例输出

2
2
2

思路:既然是求后缀个数,把字符串倒一倒就变求前缀个数了,然后就是写字典树。这题的话,map瞎搞也是能过的,耗时在800MS左右(感觉还是老老实实字典树吧~)

    拿来练字典树模板的。

字典树代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
char str[];
struct note{
note* nxt[];
int sum;//保存出现个数
note(){
sum = ;
memset(nxt,,sizeof(nxt));
}
}a[];
void insert(note *root,char s[]){
note *p = root;
int i = ;
while(s[i]){
int j = s[i++] - 'a';
if(p->nxt[j] ==NULL){
p->nxt[j] = new note();
}
p = p->nxt[j];
(p->sum) ++;//在构建的时候直接把当前结点的sum+1 说明出现过后缀
}
}
int search(note *root ,char s[]){
note *p = root;
int i = ;
while(s[i]){
int j = s[i++]-'a';
if(p->nxt[j] == NULL)return ;
p = p -> nxt[j];
}
return p -> sum;
}
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
note * root = new note();
while(n--){
scanf("%s",str);
strrev(str);
insert(root,str);
}
while(m--){
scanf("%s",str);
strrev(str);
printf("%d\n",search(root,str));
}
}
return ;
}

map代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include <sstream>
#define LL long long
using namespace std;
map<string,int>ma;
int main(){
int n,m;
string s;
char str[];
while(~scanf("%d%d",&n,&m)){
ma.clear();
while(n--){
cin>>s;
reverse(s.begin(),s.end());
while(s.size()>){
ma[s]++;
s.erase(s.size()-);
}
}
while(m--){
scanf("%s",str);strrev(str);
printf("%d\n",ma[str]);
}
}
}

TOJ3097: 单词后缀 (字典树 or map瞎搞)的更多相关文章

  1. BZOJ 4236: JOIOJI map瞎搞

    分别记录J,O,I,的个数 cnt[char][i] 表示处理到第i位,char的个数 显然当且仅当 cnt[J][i] - cnt[O][i] == cnt[J][j-1] - cnt[O][j-1 ...

  2. BestCoder Round #92 1001 Skip the Class —— 字典树 or map容器

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1001 题解: 1.trie树 关 ...

  3. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  4. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  5. POJ2945(Find the Clones)--字典树,map

    题意:给你n个规定长度的单词,问你其中出现了1次的单词,出现两次的单词...出现n次单词分别有多少个. 当然这题map也能过,但是这里介绍字典树的做法. 首相对于n个单词存入树中,当然建树过程中遇到一 ...

  6. hdu1251 字典树or map

    一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输 ...

  7. HihoCoder1366 逆序单词(字典树)

    逆序单词 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在英文中有很多逆序的单词,比如dog和god,evil和live等等. 现在给出一份包含N个单词的单词表,其中每 ...

  8. hdu2072 单词数 字典树

    字典树裸题 #include<stdio.h> #include<string.h> ][]; ]; int cnt; int ans; void Insert(char *w ...

  9. Java实现 LeetCode 720 词典中最长的单词(字典树)

    720. 词典中最长的单词 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最 ...

随机推荐

  1. js 在光标位置插入内容

    原文:https://blog.csdn.net/smartsmile2012/article/details/53642082 createDocumentFragment()用法: https:/ ...

  2. day07-多表查询

    本节重点: 多表连接查询 符合条件连接查询 子查询 准备工作:准备两张表,部门表(department).员工表(employee) create table department( id int, ...

  3. Model操作补充

    参考: http://www.cnblogs.com/wupeiqi/articles/6216618.html

  4. treeMap 基于JDK 1.8的学习

    困惑了很久的红黑树========来个了断吧 本文主要是为了描述旋转的原则,所以,至于红黑树的数据结构,红黑树的基本准则,不在强调,,红黑树困惑的就是这旋转的过程.!!! 画红黑树很简单的画图工具: ...

  5. XML报错:The reference to entity "characterEncoding" must end with the ';' delimite

    解决方法: 在web.xml增加如下配置: <filter>  <filter-name>encodingFilter</filter-name>  <fil ...

  6. XAMPP+composer+laravel+thinkphp5那些深情的往事

    xampp 依赖库 https://www.microsoft.com/zh-CN/download/details.aspx?id=29 下载地址 https://www.apachefriends ...

  7. LeetCode OJ 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  8. Ajax与select标签的组合运用

    ---------------------------------------------------------------------------------------------------- ...

  9. svg-edit和svg中的自定义属性

    用svg的码农们肯定知道,在path.rect等元数据中会加入一些自定义属性,保存于数据库,但是用svg-edit编辑器时, 读取的时候,无法读取些这些自定义属性.解决办法:找sanitize.js文 ...

  10. 吴裕雄 python深度学习与实践(3)

    import threading, time def doWaiting(): print('start waiting:', time.strftime('%S')) time.sleep(3) p ...