nyoj 题目5 Binary String Matching

Binary String Matching

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.
输出
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.
样例输入
3
11
1001110110
101
110010010010001
1010
110100010101011 
样例输出
3
0
3 

此题第一感觉就是用KMP算法,代码如下
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 char a[12];
 5 char b[1002];
 6 int next[12];
 7 
 8 void getNext() {
 9     next[0] = -1;
10     int len = strlen(a);
11     int i = 0,j = -1;
12     while(i < len) {
13         if(j == -1 || a[i] == a[j]) {
14             i++,j++;
15             next[i] = j;
16         }
17         else {
18             j = next[j];
19         }
20     }
21 }
22 
23 int calCnt() {
24     int at = 0;
25     int bt = 0;
26     int ans = 0;
27     int lena = strlen(a);
28     int lenb = strlen(b);
29     while(bt < lenb) {
30         if(at == -1 || a[at] == b[bt]) {
31             at++;
32             bt++;
33             if(at == lena) {
34                 ans++;
35                 at = next[lena];
36             }
37             continue;
38         }
39         if(a[at] != b[bt]) {
40             at = next[at];
41         }
42 
43     }
44     return ans;
45 }
46 
47 int main(int argc, char const *argv[])
48 {
49     int n;
50     while(scanf("%d",&n) != EOF) {
51         while(n--) {
52             scanf("%s",a);
53             scanf("%s",b);
54             getNext();
55             int ans = calCnt();
56             printf("%d\n", ans);
57         }
58     }
59     return 0;
60 }

此算法第一要求出next数组。而求next数组的过程本身也是一个自己和自己匹配的过程。此处用i不断前进,j不断返回,用作匹配。

计数时是新的匹配过程。和求next数组的过程神似。

若求nextval数组可能会更快些,代码如下

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 char a[12];
 5 char b[1002];
 6 int nextval[12];
 7 
 8 void getnextval() {
 9     nextval[0] = -1;
10     int len = strlen(a);
11     int i = 0,j = -1;
12     while(i < len) {
13         if(j == -1 || a[i] == a[j]) {
14             i++,j++;
15             if(i < len && a[i] == a[j]) {
16                 nextval[i] = nextval[j];
17             }
18             else {
19                 nextval[i] = j;
20             }
21             
22         }
23         else {
24             j = nextval[j];
25         }
26     }
27 }
28 
29 int calCnt() {
30     int at = 0;
31     int bt = 0;
32     int ans = 0;
33     int lena = strlen(a);
34     int lenb = strlen(b);
35     while(bt < lenb) {
36         if(at == -1 || a[at] == b[bt]) {
37             at++;
38             bt++;
39             if(at == lena) {
40                 ans++;
41                 at = nextval[lena];
42             }
43             continue;
44         }
45         if(a[at] != b[bt]) {
46             at = nextval[at];
47         }
48 
49     }
50     return ans;
51 }
52 
53 int main(int argc, char const *argv[])
54 {
55     int n;
56     while(scanf("%d",&n) != EOF) {
57         while(n--) {
58             scanf("%s",a);
59             scanf("%s",b);
60             getnextval();
61             int ans = calCnt();
62             printf("%d\n", ans);
63         }
64     }
65     return 0;
66 }

要注意15行的条件。但实际运行好像并没有更快。

今天偶然发现nyoj可以查看优秀的代码,这一点简直完爆其他oj,看到这样一种非常取巧的办法

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 int main(int argc, char const *argv[])
 6 {
 7     string s1,s2;
 8     int n;
 9     cin >> n;
10     
11     while(n--) {
12         cin >> s1;
13         cin >> s2;
14         size_t  m = s2.find(s1,0);
15         int ans = 0;
16         while(m != string::npos) {
17             ans++;
18             m = s2.find(s1,m+1);
19         }
20         cout << ans << endl;
21     }    
22     
23     return 0;
24 }

 

 

转载于:https://www.cnblogs.com/jasonJie/p/6084501.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/572988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php array =,PHP Array 函数

PHP Array 函数PHP Array 函数## PHP Array 简介PHP Array 函数允许您访问并操作数组。支持简单的数组和多维数组。## 安装PHP Array 函数是 PHP 核心的组成部分。无需安装即可使用这些函数。## PHP 5 Array 函数| 函数 | 描述 || ------------ | ------------ || array() | 创…

mac本用WTG(Windows To Go)安装Win10到移动硬盘

准备工作&#xff1a; 一个空的 USB 3.0 移动硬盘&#xff08;在安装 WTG 时候会将这个硬盘清空重新并分区&#xff0c;注意备份好数据。USB 3.0 的优盘是不行的&#xff0c;即使安装成功&#xff0c;系统的运行速度会奇慢&#xff09; 原版Windows 10 安装镜像&#xff08;建议…

mac编译安装php环境,在Mac上编译安装PHP7的开发环境

今天看到鸟哥发微博说php7 beta1测试版发布了&#xff0c;于是赶紧就去抢先下载&#xff0c;把自己的开发环境也升级到PHP7去&#xff0c;话不多少&#xff0c;下面就一起来搞起吧。。。首先你得去官网下载php7 beta1的版本这里由于我是在mac上安装&#xff0c;所以就去下载lin…

js初步简单的编程代码

简单图片切换编码demo图片地址自行替换 简单图片切换编码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml" …

AC自动机——Uva 11468 子串

题目链接&#xff1a;http://vjudge.net/contest/142513#problem/A 题意&#xff1a;给出一些字符和各自对应的选择概率,随机选择L次后将得到一个长度为L的随机字符串S.给出K个模版串,计算S不包含任何一个串的概率. 分析&#xff1a; 在构造好的AC自动机里面&#xff0c;每随机…

安卓文本编辑器php cpp,开源的Android富文本编辑器

RichEditor基于原生EditTextspan实现的Android富文本编辑器github地址&#xff1a;https://github.com/yuruiyin/RichEditor组件描述该组件是基于原生EditTextspan的方式实现的&#xff0c;旨在提供一个功能齐全且使用方便的Android富文本编辑器。主要支持了加粗斜体等行内样式…

java identifier expected,java – hibernate h2 embeddable list expected“identifier”

我试图将一个功能列表(Embeddable)关联到我的Employee Entity中,而H2似乎对这个说它期望一个“标识符”不满意Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement ”CREATE TABLE EMPLOYEE_FUNCTIONS (EMPLOYEE_EMPLOYEEID VARCHAR(255) NOT NULL,ACTIVE…

建行信用卡问题

技巧&#xff1a;一般不要打400的&#xff0c;如果有其他的固话如&#xff1a;021-38690588后按语音提示5挂失&#xff0c;点5后让输入密码&#xff0c;继续往下听&#xff0c;会提示如果遗忘密码请选择6个星*转接人工服务&#xff0c;转接后直接说明挂失等操作即可。 打021开头…

学生信息管理系统的价值PHP,php技术对学生管理系统实现的价值研究

基于php技术的学生管理系统的设计要解决的主要问题就是通过设计切实可行的管理系统来解决学校对学生信息的管理、老师对学生信息的管理、学生对自己信息的核对等问题。本系统是利用netbeans作为前台开发工具、利用phpstusy开发环境开发的&#xff0c;数据库采用MySQL&#xff0…

【FFMPEG】【ARM-Linux开发】 ffmpeg 静态库使用,undefined reference错误

原文&#xff1a;http://blog.csdn.net/chinazjn/article/details/7954984 ffmpeg移植到dm365上&#xff0c;遇到undefined reference错误&#xff1a; GA/gabin/lib/libavformat.a(allformats.o): In function av_register_all: /GA/ffmpeg-0.10/libavformat/allformats.c:53:…

php filespl,PHP SPL--遍历目录

1、PHP SPL标准库的用法(遍历目录,查找固定条件的文件)class RecursiveFileFilterIterator extends FilterIterator{// 满足条件的扩展名protected $ext array(jpg, gif);/*** 提供 $path 并生成对应的目录迭代器*/public function __construct($path){parent :: __construct(…

OpenCV 2 学习笔记(9): 定义ROI(regions of interest):给图像加入水印

http://blog.csdn.net/fred_yang2013/article/details/10175921转载于:https://www.cnblogs.com/eustoma/p/6104995.html

php 查询键名是否存在,PHP array_key_exists():检测键名是否位于数组中

PHP array_key_exists() 函数用来检查给定键名(或者索引)是否存在于数组中&#xff0c;语法如下&#xff1a;bool array_key_exists ( mixed $key , array $arr )参数说明&#xff1a;key 表示键名&#xff1b;arr 表示要被检索的数组。返回值&#xff1a;如果键名 key 存在于数…

学习笔记:MySQL字符串类型

字符串类型 a) char和varchar 1.都需要指定字符的长度&#xff0c;char中的长度是字符的长度&#xff0c;而varchar的长度是字节的长度 2. char中指定的长度就是实际占用的长度&#xff0c;而varchar指定的长度只是一个范围&#xff0c;所以varchar还要拿1-2个字节存储…

python gif 透明,Python3+试点批量处理简单的GIF到PNG并透明地去除背景色,python3Pillow,gif,转成,png,透明化,去掉...

1. 安装Pillow, 只用这个应该也可以&#xff0c;2. 安装 cImage下载后解压&#xff0c;拷贝image.py到你python安装目录的 Lib\site-packages 中。from PIL import Imageimport osimport imagedef get_imlist(path):"""返回目录中所有gif图像的文件名列表图像的…

Posix共享内存区

Posix提供了两种在无亲缘关系进程间共享内存区的方法&#xff1a; &#xff08;1&#xff09;内存映射文件&#xff1a;先有open函数打开&#xff0c;然后调用mmap函数把得到的描述符映射到当前进程地址空间中的一个文件&#xff08;上一篇笔记所用到的就是&#xff09;。 &…

matlab求半衰期,如何使用GLD和GDX价差来估计均值回归的半衰期

计算均值回归时间序列的半衰期我们可以通过例中GLD和GDX的均值回归差价来计算均值回归半衰期。MATLAB代码可以从epchan. com/book/example? _ 5. m获得。(这个程序的第一部分与example7 2. m.相同。)%在此播入example7_2. m%Insert example7 2. m in the beginning hereprevzb…

java占位符

String str"我是{0},我来自{1},今年{2}岁,{3}";String[] arr{"中国人","北京","22","谢谢"};Matcher mPattern.compile("\\{(\\d)\\}").matcher(str);while(m.find()){strstr.replace(m.group(),arr[Integer.pars…

oracle单表存储记录,oracle从各个表获得数据保存到另一个表

oracle从各个表取得数据保存到另一个表从各个表中取得数据保存另一个表中&#xff1a;CREATE VIEWPARAMETER_view ASWITHtall AS(SELECTp.PI_NO,--产品序列号p.SERIALNO,--产品编号p.PI_NAME,--产品名称p. PI_START_DATE,--产品起息日p.PI_END_DATE,--产品期日期p.PI_CUSTOMER_…

框架错误汇总

1.struts标签&#xff0c;在body中输入代码发现值栈不存在&#xff0c; 即<s:debug></s:debug>没有起作用 1 <body>2 3 4 测试url标签<br>5 <s:url value"index.jsp" var"surl"></s:url><br>6 <s:url value&…