南阳oj-----Binary String Matching(string)

题目描述:

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 

题目解析:

本题题意为在一个字符串ss中寻找子串s出现的次数

测试地址:测试点击此处

本题代码:

#include<iostream>
#include<string>
using namespace std;
void deal(string ss,string s)
{
	int sum=0,p;
	while(1)
	{
		p=ss.find(s);
		if(p==-1)
		break;
		sum++;
		ss.replace(p,1,"q");//利用替换来防止重复查找
	}
	cout<<sum<<endl;
}
int main()
{
	int n;
	string s,ss;
	cin>>n;
	while(n--)
	{
		cin>>s>>ss;
		deal(ss,s);
	}
return 0;
} 

 

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务