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. DESCryptoServiceProvider加密解密的简单使用例子

    DES.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  2. MongoDB获得短暂的

    大约MongoDB该数据是现在比较少.和大多数英文网站.最上面的经笔者从官方网站翻译.请翻译或误解之处请作证.然后,我们将继续关注MongoDB,和翻译“Developer Zone”和“Admin ...

  3. 第三章 AOP 编程选择

    Spring为我们开发者提供了多种AOP的编程方式.我们该如何选择呢? 如果项目采用的是JDK5.0以上版本,我们可以选择@AspectJ的方式.这是第一选择. http://blog.csdn.ne ...

  4. bc38 1002, bc39 1002

    比赛的时候是对于每个数,记录下来a[i], 并记录该树的下标hash[a[i]] 然后枚举a[i]的倍数,如果a[i]的倍数存在(设为k*a[i]),那么vis[k*a[i]]是不为0的 那么可以这样 ...

  5. Java的Log系统介绍和切换(转)

    Java的log系统比较繁杂.在这里梳理一下.本文只涉及log系统介绍和处理log系统之间的切换.不涉及如何配置和使用. 具体的log系统 Log4j:准确的说是log4j 1.x版.是之前使用最广泛 ...

  6. STL 源代码分析 算法 stl_algo.h -- merge

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie merge (应用于有序区间) ------------------------------ ...

  7. LVM逻辑卷管理@设备、格式、摩、引导自己主动安装一个完整的章节

    离http://www.it165.net/admin/html/201307/1553.html LVM的重要性,在这里我也就不多说了,今天和大家分享一下.LVM设备,而且安装方式. 首先呢,先让我 ...

  8. Unity3D之Vector3.Dot和Vector3.Cross采用

    在Unity3D中.Vector3.Dot表示求两个向量的点积;Vector3.Cross表示求两个向量的叉积.   点积计算的结果为数值,而叉积计算的结果为向量.两者要注意差别开来.   在几何数学 ...

  9. java.nio分析软件包(三)---Charset理解力

    前面的分析后,2一个基本的封装类型.现在我们就来揭开Java.nio魔法知识的最后一块,CharsetEncoding类,他的主要功能是实现字节Unicode之间的转换转码. 让我们来看看他同样的封装 ...

  10. WCF-001:WCF的发布

    随着“云”时代的到来,“云”已经无处不在了.什么是“云”,无非就是利用互联网强大的功能建立多个服务器,然后再利用互联网的传输数据的特点将数据从某个服务器中读取出来或者将你的数据上传上去.当然这个服务器 ...