nyoj 5 Binary String Matching(string)
Binary String Matching
- 描述
- 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#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int test, j, i;
string a, b;
int n;
cin >> test;
while(test--){
n = ;
cin >> a >> b;
int len_a = a.length(), len_b = b.length();
for(i = ; i < len_b; i++){
int k = i;
for(j = ; j < len_a; k++,j++){
if(b[k] != a[j])
break;
}
if(j == len_a)
n++;
}
cout << n << endl;
}
return ;
}上面是直接遍历。
以下代码利用find()函数
#include <iostream>
#include <string>
using namespace std; int main(){
int t;
string a, b;
cin >> t; while(t--){
cin >> a >> b;
int n = ;
int index = b.find(a, );//返回从0开始找到子串在串中的位置下标
while(index != b.npos){//npos表示不存在
n++;
index = b.find(a, index + );
}
cout << n << endl;
} return ;
}
nyoj 5 Binary String Matching(string)的更多相关文章
- String Matching(poj1580)
/*String Matching Description It's easy to tell if two words are identical - just check the letters. ...
- 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)
1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...
- 17-比赛1 B - 子串计算 Chef and his string challenge (string的运用)
Chef's best friend Jerry gives Chef a string A and wants to know the number of string A that can be ...
- 九度OJ 1094:String Matching(字符串匹配) (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1259 解决:686 题目描述: Finding all occurrences of a pattern in a text is a p ...
- 【HDOJ6629】string matching(exkmp)
题意:给定一个长为n的字符串,求其每个位置开始于其自身暴力匹配出相同或不同的结果的总比较次数 n<=1e6 思路:exkmp板子 #include<bits/stdc++.h> us ...
- C++ code:string stream(string流)
如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现? 由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据 ...
- [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧
这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...
- Java JVM 请别拿“String s=new String("z");创建了多少实例”来面试 [ 转载 ]
Java 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧 [ 转载 ] @author RednaxelaFX 原文链 ...
- 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧---转
http://www.iteye.com/topic/774673 羞愧呀,不知道多少人干过,我也干过,面壁去! 这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多 ...
随机推荐
- Quartz定时调度jar包的执行Demo分享
1.Quartz简介 Quartz框架的核心是调度器.调度器负责管理Quartz应用运行时环境.调度器不是靠自己做所有的工作,而是依赖框架内一些非常重要的部件.Quartz不仅仅是线程和线程管理. ...
- SQL 经典语句大全
原地址:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 一.基础 1.说明:创建数据库 CREATE DATABASE ...
- MongoDB一些常用指令与他的JavaScript的对应表
- [USACO09NOV]灯Lights
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
- windows 中常用的 cmd 命令汇总
查看系统基本信息: cmd -> systeminfo 或 run -> dxdiag 查询主板出厂日期: cmd -> wmic bios get releasedate 关闭本地 ...
- WCF 相关配置
WCF错误:413 Request Entity Too Large 在我们用WCF传输数据的时候,如果启用默认配置,传输的数据量过大,经常会出这个错误. WCF包含服务端与客户端,所以这个错误可能出 ...
- {Python}安装第三方包(setup.py)
在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...
- Scala简单计算实例,其在数据分析方面的优势体会
程序只是简单的从文件中读取数据,并进行计算. package com.bill.www /** * Created by Bill on 2016/2/3. * 目的:用scala实现简单的数据计算 ...
- Quartz.Net学习笔记(1)-完整的例子
一.开发环境 系统:Win10 编译器:VS2013 .Net版本:4.5 Quartz版本:2.3.3 二.涉及程序集 Common.Logging.Core.dll Common.Logging. ...
- 专题二:HTTP协议详解
我们在用Asp.net技术开发Web应用程序后,当用户在浏览器输入一个网址时就是再向服务器发送一个HTTP请求,此时就使用了应用层的HTTP协议,在上一个专题我们简单介绍了网络协议的知识,主要是为了后 ...