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
 #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)的更多相关文章

  1. String Matching(poj1580)

    /*String Matching Description It's easy to tell if two words are identical - just check the letters. ...

  2. 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)

    1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...

  3. 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 ...

  4. 九度OJ 1094:String Matching(字符串匹配) (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1259 解决:686 题目描述: Finding all occurrences of a pattern in a text is a p ...

  5. 【HDOJ6629】string matching(exkmp)

    题意:给定一个长为n的字符串,求其每个位置开始于其自身暴力匹配出相同或不同的结果的总比较次数 n<=1e6 思路:exkmp板子 #include<bits/stdc++.h> us ...

  6. C++ code:string stream(string流)

    如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现? 由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据 ...

  7. [转] 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧

    这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多已经吐槽无力,但这次实在忍不住了就又爆发了一把.写得太长干脆单独开了一帖. 顺带广告:对JVM感兴趣的同学们同志们 ...

  8. Java JVM 请别拿“String s=new String("z");创建了多少实例”来面试 [ 转载 ]

    Java 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧 [ 转载 ] @author RednaxelaFX 原文链 ...

  9. 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧---转

    http://www.iteye.com/topic/774673 羞愧呀,不知道多少人干过,我也干过,面壁去! 这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多 ...

随机推荐

  1. [转载]android常用的API接口调用

    原文地址:android常用的API接口调用作者:宋耀 显示网页:         Uri uri = Uri.parse("http://www.google.com"); In ...

  2. bzoj 1598: [Usaco2008 Mar]牛跑步【A*K短路】

    A*K短路模板,详见https://blog.csdn.net/z_mendez/article/details/47057461 算法流程: 把有向图全建成反向边,跑一遍所有点到t的最短路记为dis ...

  3. Storm概念学习系列之核心概念(Tuple、Spout、Blot、Stream、Stream Grouping、Worker、Task、Executor、Topology)(博主推荐)

    不多说,直接上干货! 以下都是非常重要的storm概念知识. (Tuple元组数据载体 .Spout数据源.Blot消息处理者.Stream消息流 和 Stream Grouping 消息流组.Wor ...

  4. MVC学习-用EF做增删改查

    在做增删改查先,先介绍几个知识点: 1.代理类 在将对象方法EF数据上下文时,EF会为该对象封装 一个代理类对象, 同时为该对象的每一个属性添加一个标志:unchanged, 当对该对象某个属性进行操 ...

  5. 北工大2017校赛 1101:要打车的FanZzz

    题目链接: http://bjutacm.openjudge.cn/lianxi/1101/ 思路: 二分 + 二分图最大匹配. 开始的时候我想直接用最小费用流模型,后来发现这样是错误的.因为这道题实 ...

  6. Framework7首页隐藏navbar

    f7首页隐藏navbar其他页面显示navbar 帮别人解决问题,自己也记录一下, 首页.navbar加.navbar-hidden, 首页.page加.no-navbar, 如果首页有.navbar ...

  7. Android常见问题总结(二)

    1.布局文件LinearLayout线性布局添加内容报错. 解决方法: 线性布局LinearLayout中包裹的元素多余1个需要添加android:orientation属性. 2.android 的 ...

  8. Maven对不同的测试环境用不同的参数进行打包

    通过mvn package -P ${env} 加载不同配置文件 1.pom.xml中的配置 filter-dev.properties jdbc.properties

  9. mongodb GUI工具

    人性化,界面工具 网上搜索找的一些 1. 官方网站 tools 2. adminMongo 这个也是我在使用的 3. MongoClient 4. NoSQL Manager for MongoDB ...

  10. react Native环境 搭建

    react Native的优点:跨平台 低投入高回报 性能高 支持动态更新.一才两用(ios和Android) 开发成本第 代码复用率高.windows环境搭建react Native开发环境1.安装 ...