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> char a[];
char b[];
int next[]; void getNext() {
next[] = -;
int len = strlen(a);
int i = ,j = -;
while(i < len) {
if(j == - || a[i] == a[j]) {
i++,j++;
next[i] = j;
}
else {
j = next[j];
}
}
} int calCnt() {
int at = ;
int bt = ;
int ans = ;
int lena = strlen(a);
int lenb = strlen(b);
while(bt < lenb) {
if(at == - || a[at] == b[bt]) {
at++;
bt++;
if(at == lena) {
ans++;
at = next[lena];
}
continue;
}
if(a[at] != b[bt]) {
at = next[at];
} }
return ans;
} int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
while(n--) {
scanf("%s",a);
scanf("%s",b);
getNext();
int ans = calCnt();
printf("%d\n", ans);
}
}
return ;
}

此算法第一要求出next数组。而求next数组的过程本身也是一个自己和自己匹配的过程。此处用i不断前进,j不断返回,用作匹配。

计数时是新的匹配过程。和求next数组的过程神似。

若求nextval数组可能会更快些,代码如下

 #include <cstdio>
#include <cstring> char a[];
char b[];
int nextval[]; void getnextval() {
nextval[] = -;
int len = strlen(a);
int i = ,j = -;
while(i < len) {
if(j == - || a[i] == a[j]) {
i++,j++;
if(i < len && a[i] == a[j]) {
nextval[i] = nextval[j];
}
else {
nextval[i] = j;
} }
else {
j = nextval[j];
}
}
} int calCnt() {
int at = ;
int bt = ;
int ans = ;
int lena = strlen(a);
int lenb = strlen(b);
while(bt < lenb) {
if(at == - || a[at] == b[bt]) {
at++;
bt++;
if(at == lena) {
ans++;
at = nextval[lena];
}
continue;
}
if(a[at] != b[bt]) {
at = nextval[at];
} }
return ans;
} int main(int argc, char const *argv[])
{
int n;
while(scanf("%d",&n) != EOF) {
while(n--) {
scanf("%s",a);
scanf("%s",b);
getnextval();
int ans = calCnt();
printf("%d\n", ans);
}
}
return ;
}

要注意15行的条件。但实际运行好像并没有更快。

今天偶然发现nyoj可以查看优秀的代码,这一点简直完爆其他oj,看到这样一种非常取巧的办法

 #include <iostream>
#include <string>
using namespace std; int main(int argc, char const *argv[])
{
string s1,s2;
int n;
cin >> n; while(n--) {
cin >> s1;
cin >> s2;
size_t m = s2.find(s1,);
int ans = ;
while(m != string::npos) {
ans++;
m = s2.find(s1,m+);
}
cout << ans << endl;
} return ;
}

nyoj 题目5 Binary String Matching的更多相关文章

  1. NYOJ之Binary String Matching

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

  2. NYOJ 5 Binary String Matching

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

  3. nyoj 5 Binary String Matching(string)

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

  4. Binary String Matching

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

  5. ACM Binary String Matching

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

  6. Binary String Matching(kmp+str)

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

  7. 【ACM】Binary String Matching

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

  8. NYOJ5——Binary String Matching

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

  9. NYOJ5 Binary String Matching

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

随机推荐

  1. linux 命令——31 /etc/group文件(转)

    Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件. linux /etc/group文件是有关于系统管理员对用 ...

  2. 【洛谷2522】[HAOI2011] Problem b(莫比乌斯反演)

    点此看题面 大致题意: 求\(\sum_{x=a}^b\sum_{y=c}^d[gcd(x,y)==k]\). 关于另一道题目 在看这篇博客之前,如果你做过一道叫做[BZOJ1101][POI2007 ...

  3. 【转】 Solr的SolrCloud与Master-slave主从模式对比

    第一印象 SolrCloud是Solr4.0引入的,主要应对与商业场景.它很像master-slave,却能自动化的完成以前需要手动完成的操作.利用ZooKeeper这个工具去监控整个Solr集群,以 ...

  4. 5-15 笔记 jtopo使用

    Jtopo的核心对象有6个,分别是Stage(舞台对象),Scene(场景对象),Node(节点对象),Link(连线对象),Container(容器对象),Effect.Animate(动画效果) ...

  5. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第二节

    原文链接 第二节:第一个内核 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进行大型并 ...

  6. ambari过程中要求各个节点时间同步

    设置时间同步 控制节点机器 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #设置时区为北京时间,这里为上海,因为centos里面只有上海... ...

  7. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  8. 1061: [Noi2008]志愿者招募

    Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5742  Solved: 3449[Submit][Status][Discuss] Descript ...

  9. 安装Tesseract

    下载网站 https://digi.bib.uni-mannheim.de/tesseract/

  10. 十四、MySQL UPDATE 查询

    MySQL UPDATE 查询 如果我们需要修改或更新 MySQL 中的数据,我们可以使用 SQL UPDATE 命令来操作.. 语法 以下是 UPDATE 命令修改 MySQL 数据表数据的通用 S ...