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 ...
随机推荐
- eclipse 软件的背景颜色、字体设置
1.eclipse 背景色设置: Window->Preferences->General->Editors->Text Editors->Backgroud color ...
- 一个AVRUSB作品HID类
最近为了HID类设备弄了半个把月.终于完成这个无线(红外)多媒体键盘,带鼠标功能.难就难在描述符上,不过在近4天的资料翻阅及多次的测试下,电脑不知道重启了多少次啊,的情况下 搞定了这个东西,大家可以看 ...
- 商人过河问题(二)java实现
本文实现的java版商人过河是参考http://wenku.baidu.com/link?url=dpe2AC8mCjpGnclFv6iZy88_vqYm3bED4QDpSkAI4ssgs7Bhntu ...
- 模板应用--UI线程与worker线程同步 模仿c# invoke
由之前的一篇博文 <UI线程与worker线程><UI线程与worker线程>引出,UI线程与worker线程“串行化”在win32上实现是多么没有节操的事情,代码编写麻烦不说 ...
- Android 菜单(OptionMenu)大全 建立你自己的菜单
转自:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html 菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单 ...
- stage.width/height和stage.stageWidth/stageHeight的区别
stage.stageWidth和stage.stageHeight就是舞台的宽带和高度 一般默认打开宽带是550,高度是400 那么stage.stageWidth=550,stage.stageH ...
- 【字母树+贪心】【HDU3460】【Ancient Printer】
题目大意: 一个打印机 只有 打印,删除,a-z.操作 给你一堆队名,如何才能操作次数最少输出全部 (字典树节点数-1)*2 输入,删除操作数 字符串数 printf操作数 最长字符串的长度 最后一个 ...
- LeetCode Day2
Power of Two /** * LeetCode: Power of Two * Given an integer, write a function to determine if it is ...
- aps.net js获取服务器控件
document.getElementById("<%= tx_ownerId.ClientID %>").value;
- myql 注意事项
在[mysqld]下加入一行:lower_case_table_names=1,1为不区分大小写,0是区分大小写...并/etc/init.d/mysql restart即可...