描述

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

 
输入
第一行包含一个整数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. Android研究之手PullToRefresh(ListView GridView 下拉刷新)使用具体解释

     群里一哥们今天聊天偶然提到这个git hub上的控件:pull-to-refresh ,有兴趣的看下,样例中的功能极其强大,支持非常多控件.本篇博客具体给大家介绍下ListView和GridVi ...

  2. 最近做RTSP流媒体的实时广播节目

    //h264视频流打包代码 // NALDecoder.cpp : Defines the entry point for the console application. #include < ...

  3. CentOS7下一个mysql安装

    CentOS7安装MySQL --下载mysql http://mirrors.sohu.com/mysql/MySQL-5.6/ http://mirrors.sohu.com/mysql/MySQ ...

  4. sails不是内部或外部命令的解决方案

    1 安装好node 2 安装sails 打开cmd窗口,用命令 npm -g install sails 安装sails 安装完成后,用命令  sails new testProject 创建项目 会 ...

  5. WebLogic部署集群和代理服务器

    应公司要求,最近在学习weblogic集群这块的知识,下面我把我这几天学到的,以及过程中遇到的问题及如何解决的,分享给大家.首先,weblogic是Orcale公司的一款产品,至于其作用,我想就不用我 ...

  6. perl操作sqlserver实现BCP

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

  7. ./startup.sh: Permission denied

    今天在Linux上部署项目,之前报过这个错误,通过下面方式攻克了.如今发出来备份一下: LINUX权限-bash: ./startup.sh: Permission denied 运行./startu ...

  8. js比量undefined种类

    js比量undefined种类 if (reValue== undefined) {     alert("undefined"); } 发现推断不出来.最后查了下资料要用type ...

  9. linux内存操作--ioremap和mmap学习笔记

    最近做一些相关的视频输出,对于保留framebuffer内存使用情况不是很清楚,现在找了一些资料整理出,准备使用.if (希望看到使用)  goto   用法: 对于一个系统来讲,会有非常多的外设,那 ...

  10. 【BZOJ1791】【IOI2008】【基环树】island(status第一速度)

      1791: [Ioi2008]Island 岛屿  Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 908  Solved: 159 [Su ...