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
/******KMP算法*********/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
//#define MAXSTRLEN 1000
char s[1010];
char t[11];
int next[1010];
using namespace std;
void Next()
{
int i=0;
int j=-1;
next[0]=-1;
int len_t=strlen(t);
while(i<len_t-1)
{
if(j==-1||t[i]==t[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int KMP()
{
int i=-1;
int j=-1;
int len_t=strlen(t);
int len_s=strlen(s);
int count=0;
Next();
while(i<len_s)
{
if(j==-1||s[i]==t[j])
{
i++;
j++;
}
else
{
j=next[j];
}
if(j==len_t)
{
i=i-j;
j=-1;
count++;
}
}
return count;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s %s",t,s);
printf("%d\n",KMP());
}
return 0;
}

版权声明:本文博主原创文章,可能不会没有任何同意转载!

NYoj-Binary String Matching-KMP算法的更多相关文章

  1. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  2. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  3. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  4. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. nyoj 5 Binary String Matching(string)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. 【ACM】Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  7. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  8. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. NYOJ5——Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3  描述:Given two strings A and B, whose alph ...

  10. NYOJ5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

随机推荐

  1. Unity3D发布WebPlayer时Failed to download data file解决方案

    今天发布WebPlayer时, 发现直接打开html是可以正常运行的, 但是通过iis访问的话就会报错: Failed to download data file. 一开始以为是防火墙, 后来发现不是 ...

  2. js创建下载文件

    function downloadFile(fileName, content){ var aLink = document.createElement('a'); var blob = new Bl ...

  3. android定位方式

    做移动互联网就不太可能不碰手机端的开发.上周为了项目需要,俺也挽袖子抡胳膊开始写起了android程序,还好有java基础,倒也上手快,写了几个小程序,主要都是关于定位方面的. 网上也搜得到一些相关的 ...

  4. lua 远程调试 【zeroBrane 使用mobdebug】(good转)

    最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...

  5. SharePoint 2013 创建web应用程序报错&quot;This page can’t be displayed&quot;

    错误描写叙述 This page can't be displayed •Make sure the web address http://centeradmin is correct. •Look ...

  6. 【Linux探索之旅】第二部分第四课:文件操纵,鼓掌之中

    内容简介 1.第二部分第四课:文件操纵,鼓掌之中 2.第二部分第五课预告:用户和权限 文件操纵,鼓掌之中 既然上一课我们学习了Linux中的文件组织方式,那么现在就该是玩弄,啊不,是操纵它们的时候了. ...

  7. jquery省市区三级联动

    jquery省市区三级联动(数据来源国家统计局官网)内附源码下载 很久很久没有写博了. 今天更新了项目的省市区三级联动数据,更新后最新的海南三沙都有,分享给所有需要的小伙伴们... JQUERY + ...

  8. OpenJDK 阅读源代码 Java 实现字节流输入类

    Java 的输入输出总是给人一种非常混乱的感觉.要想把这个问题搞清楚.必须对各种与输入输出相关的类之间的关系有所了解. 仅仅有你了解了他们之间的关系.知道设计这个类的目的是什么.才干更从容的使用他们. ...

  9. WebService 简单安全验证

    原文:WebService 简单安全验证 最近新接了一个需要调用第三方WebService的项目,看到这个第三方WebService被调用的时候,需要授权用户名和密码,于是自己也想对WebServic ...

  10. Poj3414广泛搜索

    <span style="color:#330099;">/* D - D Time Limit:1000MS Memory Limit:65536KB 64bit I ...