NYoj 685 查找字符串
描述
小明得到了一张写有奇怪字符串的纸,他想知道一些字符串出现了多少次,但这些字符串太多了,他想找你帮忙,你能帮他吗?输入字符包括所有小写字母、‘@’、‘+’。
- 输入
- 第一行包含一个整数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 查找字符串的更多相关文章
- 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)
说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...
- php查找字符串首次出现的位置 判断字符串是否在另一个字符串中
strpos - 查找字符串首次出现的位置 说明 int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 返回 nee ...
- 查找字符串的 KMP 算法
查找字符串是我们平常编程过程中经常遇到的,现在介绍一种查找字符串算法,增加程序的执行速度. 通常我们是这么写的: /* content: search a string in a othor stri ...
- 回朔法/KMP算法-查找字符串
回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...
- Lua查找字符串注意
问题: 使用Lua写Wireshark插件时,经常匹配字符串.今天使用string.find()函数查找字符串”max-age”,没有找到. 分析: local index = string.find ...
- Javascript 查找字符串中出现最多的字符和出现的次数
<script type="text/javascript"> //查找字符串中出现最多的字符和出现的次数 var str = 'Thatwheneying its o ...
- 查找字符串(C++实现)
查找字符串(C++实现),不使用库函数: // SubString.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...
- php查找字符串是否存在
strstr //搜索字符串在另一字符串中的首次出现(对大小写敏感) //该函数返回字符串的其余部分(从匹配点).如未找到则返回 false stristr //查找字符串在另一字符串中第一次出现的位 ...
- linux上查找文件存放地点和文件中查找字符串方法
一.查找文件存放地点 1.locate 语法:locate <filename> locate命令实际是"find -name"的另一种写法,但是查找方式跟find不同 ...
随机推荐
- Android毛玻璃处理代码(Blur)
以下为将bitmap图像处理为毛玻璃效果的图像的工具类: public class FastBlurUtil { public static Bitmap doBlur(Bitmap sentBitm ...
- [LeetCode238]Product of Array Except Self
题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 玩转Web之Json(二)----jquery easy ui + Ajax +Json+SQL实现前后台数据交互
最近在学Json,在网上也找过一些资料,觉得有点乱,在这里,我以easy ui的登录界面为例来说一下怎样用Json实现前后台的数据交互 使用Json,首先需要导入一些jar包,这些资源可以在网上下载到 ...
- 再谈Hibernate级联删除——JPA下的Hibernate实现一对多级联删除CascadeType.DELETE_ORPHAN
声明: 1.本文系原创,非抄袭或转载过来的. 2.本文论点都亲手做过实验论证. 3.本文所讲的Hibernate配置都基于注解的方式,hbm语法未提供. 非常多人对持久层概念搞不清JPA.Hibern ...
- java代码 分解EXCEL(一)
一,service层接口定义: ExcelParseService.java 一,service层接口实现: ExcelParseServiceImpl.java watermark/2/text/a ...
- groovy : poi 导出 Excel
參考 poi-3.10-FINAL/docs/spreadsheet/quick-guide.html write_xls.groovy 代码例如以下 package xls; import java ...
- Linux课程_系统配置和日常维护
1.设置命令输入提示格公式:"username:当前文件夹$" 2.设置命令输入提示行格式为:"当前系统时间-用户#"(提示:Shell将通过反引號" ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- Chrome 扩展 最近的历史 HistoryBar v1.1
说明 以前用过一段时间傲游浏览器,渐渐的习惯了它的鼠标手势和一些细微的人性化的功能.比方地址栏左边的"近期訪问的页面"button.能够方便的找到近期 20 条历史记录. wate ...
- 编程基础——C/C++,Java,ObjC讨论回调模式
什么是回调? 因为它是从C开始进入编程世界.术语改只是口.叫习惯了.java里通常叫listener(监听器).C/C++里通常叫callback(回调),ObjC里面叫delegate(托付) 回调 ...