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 命令——46 vmstat(转)

    vmstat 是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...

  2. flush caches

  3. 闭包 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个对Array的求和.通常情况下,求和的 ...

  4. oc字符串截取 数组字典运用

    #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS_ ...

  5. SpringBoot之YAML

    SpringBoot的配置文件有两种,一种是properties结尾的,一种是以yaml或yml文件结尾的 我们讨论一下yml文件结尾的文件: 基本语法: 其实yml文件就是键值对的形式,不过就是键( ...

  6. pm2 服务器命令

    1..配置日志文件路径 命令:pm2 start /home/admin/node/fotonIp/bin/www  --name ip -i 4  -o   "/app/node/logs ...

  7. selenium 双击元素

    #定位元素 pod_input = driver.find_element(By.ID, 'j_idt9:searchForm:j_idt11:toSelectorLocation:toSelecto ...

  8. 开发监测keepalived裂脑的脚本

    检测思路:在备节点上执行脚本,如果可以ping通主节点并且备节点有VIP就报警,让人员介入检查是否裂脑. 在LB02备节点上开发脚本并执行: [root@lb02 ~]# cat /server/sc ...

  9. SpringSecurity项目报错

    启动时,提示: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory be ...

  10. 小程序wafer2操作数据库

    小程序操作数据库 //小程序控制台phpmyadmin里给数据库cAuth添加表 //controllers/hello.js const { mysql } = require('../qcloud ...