Binary String Matching(kmp+str)
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
题解:串a在串b中出现多少次,简单kmp,strstr函数也可以;
strstr代码:#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
char A[],B[MAXN];
int main(){
int N;
SI(N);
while(N--){
scanf("%s%s",A,B);
int ans=,t=-;
while(true){
if(strstr(B+t+,A))t=strstr(B+t+,A)-B;
else break;
ans++;
}
printf("%d\n",ans);
}
return ;
}kmp
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdlib>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
char A[],B[MAXN];
int p[MAXN];
void getp(){
int i=,j=-;
p[]=-;
int len=strlen(A);
while(i<len){
if(j==-||A[j]==A[i]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
int kmp(){
getp();
int i=,j=;
int len=strlen(B);
int ans=;
while(i<len){
if(j==-||B[i]==A[j]){
i++;j++;
if(j==strlen(A))ans++;
}
else j=p[j];
}
return ans;
}
int main(){
int T;
SI(T);
while(T--){
scanf("%s%s",A,B);
printf("%d\n",kmp());
}
return ;
}
Binary String Matching(kmp+str)的更多相关文章
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- 【ACM】Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ 5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ5——Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:Given two strings A and B, whose alph ...
- nyoj 5 Binary String Matching(string)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- 【Sqlserver清空数据库中所有表数据】
脚本: CREATE PROCEDURE sp_DeleteAllData AS EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT AL ...
- Inno Setup技巧[实例]添加自定义页面
原文 http://hi.baidu.com/watashi/item/b3dda993459ff8f0291647a0 通过“添加自定义页面”可以丰富安装程序的功能.本文以添加一个页面“选择安装类型 ...
- java设计模式--行为型模式--迭代模式
迭代器模式 概述 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 适用性 .访问一个聚合对象的内容而无需暴露它的内部表示. .支持对聚合对象的多种遍历 ...
- thrift TNonblockingServer 使用
下载 0.9.1 版本 (0.9.2需要 2.5的bison,而 RHEL6上自带bison是2.4) TNonblockingServer 时必须使用 TFramedTransport ,不能使 ...
- poj1966Cable TV Network(无向图最小点割集 ISAP+邻接矩阵)
题目请戳这里 邻接表的ISAP被卡了一天...TLE....终于被卡了...好忧桑啊啊啊... 题目大意:给一张无向图,求最少去掉几个点使图不连通. 题目分析:求无向图的点连通度,拆点建图跑最大流.具 ...
- poj 2531 Network Saboteur(经典dfs)
题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值. 思路:1.把这两个集合标记为0和1,先默认所有点都在集合0里. 2 ...
- 你知道为什么Xcode6中Swift没有智能提示和自己主动补全功能吗 ?
你知道为什么Xcode6中Swift没有智能提示和自己主动补全功能吗 ? 长沙戴维营教育将为你解开这个巨大的谜团大BUG! http://www.ubuntucollege.cn/course/29/ ...
- 关于OF和CF
很久很久前写的.越来越意识到作为一名科班出身的学生的重要性. 自己在使用IDA时,发现F5产生类似的这种代码. 其中有一句,v5 <= -141920797,我在想为什么是负数.如果把-1419 ...
- 密码算法详解——DES
0 DES简介 在20世纪60年代后期,IBM公司成立了一个由Horst Feistel负责的计算机密码学研究项目.1971年设计出密码算法LUCIFER后,该项目宣告结束.LUCIFER被卖给了伦敦 ...
- 《JavaScript 闯关记》之简介
简介 JavaScript 是面向 Web 的编程语言,绝大多数现代网站都使用了 JavaScript,并且所有的现代 Web 浏览器(电脑,手机,平板)均包含了 JavaScript 解释器. 这使 ...