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笔试题的. 本来因为见得太多 ...
随机推荐
- [App Store Connect帮助]三、管理 App 和版本(7)移除 App
若要在“我的 App”主视图中移除 App,要先将此 App 从 App Store 中移除,并将所有与之关联的 App 内购买项目下架. 此外,仅当 App 的所有版本均处于下列状态之一时方可移除: ...
- [Usaco2018 Open]Disruption
Description Farmer John自豪于他所经营的交通发达的的农场.这个农场是由N块牧场(2≤N≤50,000)组成的,N-1条双向道路将它们连接起来,每一条道路的都为一单位长度.Farm ...
- 状压DP UVA 11795 Mega Man's Mission
题目传送门 /* 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- Asp.net:MVC认识
用MVC框架好长一段时间,发现每天都是写业务代码,不想每天只为了工作而写代码,想把工作中认识的MVC框架,遇到的问题,有时候天天在用,但是不知道里面是什么东西,什么原理,为啥這样写等一系列问题.进行梳 ...
- mysql若干问题
一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...
- Objective-C设计模式——单例Singleton(对象创建)
单例 和其它语言的单例产不多,可以说是最简单的一种设计模式了.但是有几个点需要注意下,单例就是一个类只有一个实例. 所以我们要想办法阻止该类产生别的实例,一般语言中都会将构造函数写为private.但 ...
- [ TJOI 2007 ] 线段
\(\\\) \(Description\) 一个\(N\times N\) 的网格,每行有一段要必走,求从\((1,1)\)到\((N,N)\)的最短路长度. \(N\le 2\times10^4\ ...
- P2668 斗地主 dp+深搜版
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- FCC 基础JavaScript 练习1
1.JavaScript中的注释方式有以下两种 // This is an in-line comment. /* This is a multi-line comment */ 2.avaScrip ...