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. 官网下载qt-opensource-windows-x86-mingw482_opengl-5.3.1.exe。封装好了Qt libraries、Qt Creator。只需要这一个可执行程序就好了。

    官网下载qt-opensource-windows-x86-mingw482_opengl-5.3.1.exe( http://qt-project.org/downloads).这个可执行程序已经为 ...

  2. GlusterFS源代码解析 —— GlusterFS 简单介绍

    原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564185 -- -- 本系列博客源代码是基于GlusterFS 3 ...

  3. It's only too late if you decide it is. Get busy living, or get busy dying(转)

    我们的身边是不是有这样的一群人,他们心肠不坏,容易感伤,但吐槽似乎是常有的事情,看着时下电视电影,说起哪些哪些自己的泪点.一般都是吃货,别人失眠的时候,TA可能在回忆自己刚做了什么梦.我不是要去评论这 ...

  4. iOS游戏开发游戏功能之外的东西

    对于一个游戏的开发,我们除了完毕游戏的功能之外,还有多少东西我们须要考虑呢? 非常多.也非常烦! 但做过一遍之后下一次就会非常easy. 都有什么东西我们想加入到游戏其中呢? (1)分享功能 (2)评 ...

  5. flex4 amcharts 删除水印

    package com.twobowl.component.manualTopo { import com.amcharts.AmPieChart; import com.amcharts.chart ...

  6. SAP ABAP计划 SY-REPID与SY-CPROG差异

    首先,它的两个解释   sy-repid is the name of the current program.  "当前程序的程序名                             ...

  7. 从字节码层面看“HelloWorld” (转)

    一.HelloWorld 字节码生成 众所周知,Java 程序是在 JVM 上运行的,不过 JVM 运行的其实不是 Java 语言本身,而是 Java 程序编译成的字节码文件.可能一开始 JVM 是为 ...

  8. Memento pattern

    21.5 再谈备忘录的封装 备忘录是一个很特殊的对象,只有原发器对它拥有控制的权力,负责人只负责管理,而其他类无法访问到备忘录,因此我们需要对备忘录进行封装. 为了实现对备忘录对象的封装,需要对备忘录 ...

  9. LNMP 免安装包

    LNMP(Linux-Nginx-Mysql-PHP)可爱的黄金搭档,不过配置并不轻易,而我平常用于测试环境又经常用到,所以打包了这么一个免安装的LNMP包,内置常用库和模块,以及基本的优化设置,这样 ...

  10. Windows+Atlassian-Jira-6.0.4+MySql5.0安装破解汉化

     Windows+Atlassian-Jira-6.0.4+MySql5.0安装破解汉化 一:整理的安装程序 例如以下图: 文件太大.上传不到csdn上.有须要的联系. 新增的百度云盘下载:链接: ...