描述

小明得到了一张写有奇怪字符串的纸,他想知道一些字符串出现了多少次,但这些字符串太多了,他想找你帮忙,你能帮他吗?输入字符包括所有小写字母、‘@’、‘+’。

 
输入
第一行包含一个整数T(T<=100).表示测试数据组数。
接下来每组数据第一行包含两个整数n,m(n,m<100000),分别表示有n个字符串,小明要问你m次。
接下来n行,每行包含一个字符串,长度不大于15。
接下来m行,每行包含一个字符串,表示小明要问该串出现的次数。
输出
输出每组小明询问数串出现的次数。
样例输入
1
5 3
hello
it@is+so@easy
hello
ibelieveicanac
hello
hello
icannotacit
Giveup
样例输出
3
0
0 题解:水题,就是多判断了“+”,“@”的情况,修改一下模板即可。 代码:
 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; struct node
{
int count;
node *next[];
node(){ //构造函数
count=;
memset(next,,sizeof(next));
}
};
node *root;
int k=;
void insert(char *a)
{
int l=strlen(a);
node *p=root;
int i;
for(i=;i<l;i++)
{
if(p->next[a[i]-'+']==)
{
p->next[a[i]-'+']=new node;
}
// 已存在此前缀
p=p->next[a[i]-'+'];
p->count++;
}
}
int find(char *s)
{
struct node *p;
int len=strlen(s);
if(len==) return ;
p=root;
for(int i=;i<len;i++){
if(p->next[s[i]-'+']!=)
p=p->next[s[i]-'+'];
else
return ;
}
return p->count;
}
void de(node *p)
{
if(p==)
return ;
int i;
for(i=;i<;i++)
{
de(p->next[i]);
}
delete p; }
int main()
{
int t;
scanf("%d",&t);
char a[],b[];
while(t--)
{
root = new node;
int n,m;
k=;
scanf("%d%d",&n,&m);
int i;
for(i=;i<n;i++)
{
scanf("%s",a);
insert(a);
}
for(i=;i<m;i++){
scanf("%s",b);
int ans=;
ans=find(b);
printf("%d\n",ans);
}
de(root);
}
return ;
}

NYoj 685 查找字符串的更多相关文章

  1. 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)

    说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...

  2. php查找字符串首次出现的位置 判断字符串是否在另一个字符串中

    strpos - 查找字符串首次出现的位置 说明 int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 返回 nee ...

  3. 查找字符串的 KMP 算法

    查找字符串是我们平常编程过程中经常遇到的,现在介绍一种查找字符串算法,增加程序的执行速度. 通常我们是这么写的: /* content: search a string in a othor stri ...

  4. 回朔法/KMP算法-查找字符串

    回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...

  5. Lua查找字符串注意

    问题: 使用Lua写Wireshark插件时,经常匹配字符串.今天使用string.find()函数查找字符串”max-age”,没有找到. 分析: local index = string.find ...

  6. Javascript 查找字符串中出现最多的字符和出现的次数

    <script type="text/javascript"> //查找字符串中出现最多的字符和出现的次数 var str = 'Thatwheneying its o ...

  7. 查找字符串(C++实现)

    查找字符串(C++实现),不使用库函数: // SubString.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...

  8. php查找字符串是否存在

    strstr //搜索字符串在另一字符串中的首次出现(对大小写敏感) //该函数返回字符串的其余部分(从匹配点).如未找到则返回 false stristr //查找字符串在另一字符串中第一次出现的位 ...

  9. linux上查找文件存放地点和文件中查找字符串方法

    一.查找文件存放地点 1.locate 语法:locate <filename> locate命令实际是"find -name"的另一种写法,但是查找方式跟find不同 ...

随机推荐

  1. 设计师给了px显着的单位,Android要设置多少开发商dip、dp、sp?

    此链接    http://blog.csdn.net/xiaodongrush/article/details/29560431 1. 要开发一款Android APP,设计师和开发要约定哪些事情? ...

  2. 输出无名空数组---精android、IOS App应用服务程序开发

    直接输出 [] 示例文件_samples/app/array_null.json在轻开平台的_samples/app/文件夹下 太Easy.无法写出很多其它的内容,大家还是自己试试吧! ! ! 相关资 ...

  3. Spring Assert主张 (参议院检测工具的方法-主张)

    Web 收到申请表格提交的数据后都需要对其进行合法性检查,假设表单数据是不合法的,该请求将被拒绝.分类似的,当我们写的类方法,该方法还经常需要组合成参 法国检查.假设参议院不符合要求,方法通过抛出异常 ...

  4. 认识Backbone (二)

    Backbone.Model(模型) Models(模型)是任何Javascript应用的核心,包括数据交互及与其相关的大量逻辑: 转换.验证.计算属性和访问控制.Model在Backbone中为数据 ...

  5. perl操作sqlserver实现BCP

    #!C:\Perl64\bin #由BCP备份和恢复SQLSERVER指定表 use 5.014; #加载用户和password型材 my $username ; my $passwd; ##得到us ...

  6. springMVC项目异步处理请求的错误Async support must be enabled on a servlet and for all filters involved in async

    从github上down下来一个项目,springMVC-chat.作者全是用的注解,也就是零配置.这可苦了我,经过千辛万苦,终于集成到如今的项目中有一点样子了,结果报出来以下的错误.红色部分.解决方 ...

  7. ZendStudio10.6.1如何安装最新的集成svn小工具?

    选择Help菜单->Install New Software...在Work with输入http://subclipse.tigris.org/update_1.10.x,等待完成后,.除了S ...

  8. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use &#39;track by&#39; expression to specify uniq

    angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...

  9. epoll演示样本

    server参考是别人的代码 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include ...

  10. Unity3d C# Socket 下载文件 (同步到)

    续篇 Unity3d C# HttpWebRequest 异步下载文件 ,由于project编译为IL2CPP的情况下仍然无效.提示HttpWebrequest 在当前版本号不支持.所以还是寻求其他的 ...