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. 测试发布(maven-assembly-plugin看好你哦)

    项目改成了maven管理,现场需要用增量补丁包的形式发布代码: 2015/4/21 以前试过用ant打补丁包,现在试试maven能不能做同样的事情: maven-assembly-plugin看着可以 ...

  2. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  3. 线段树和zkw线段树

    作者作为一个蒟蒻,也是最近才自学了线段树,不对的地方欢迎大佬们评论,但是不要喷谢谢 好啦,我们就开始说说线段树吧 线段树是个支持区间操作和查询的东东,平时的话还是蛮实用的 下面以最基本的区间加以及查询 ...

  4. python实现批量修改文件名

    import os def dele(): # 设置一个计数器 n=0 st = input('请输入你要删除的字符:') for i in f: b = f[n] if st in b: oldna ...

  5. hprose 1.0(rpc 框架) - 关于跨域和P3P的声明

    private function sendHeader($context) { if ($this->onSendHeader !== null) { $sendHeader = $this-& ...

  6. vmware 开机自动启动

    vmware开机自动启动, 可以使用vmrun命令. 1. 首先在“我的电脑”-“属性”-“高级”-“环境变量”-“PATH”中添加vmware路径,如:C:\Program Files (x86)\ ...

  7. Laravel — homestead 配置多站点

    一.homestead.yaml 配置 homestead.yaml 文件配置sites,如下 sites: - map: homestead.test to: /home/vagrant/Code/ ...

  8. mysql同步故障解决

    故障现象:Slave_SQL_Running: No Slave状态:mysql> show slave status\GSlave_IO_Running: YesSlave_SQL_Runni ...

  9. Java基础知识:Collection接口

    *本文是最近学习到的知识的记录以及分享,算不上原创. *参考文献见文末. 这篇文章主要讲的是java的Collection接口派生的两个子接口List和Set. 目录 Collection框架 Lis ...

  10. Diycode开源项目 NotificationActivity

    1.NotificationActivity预览以及布局详解 1.1.首先看一下通知的具体页面. 1.2.然后是布局代码==>activity_fragment.xml <LinearLa ...