nyoj 题目5 Binary String Matching
Binary String Matching
- 描述
- 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的更多相关文章
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- NYOJ 5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- nyoj 5 Binary String Matching(string)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 【ACM】Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- NYOJ5——Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:Given two strings A and B, whose alph ...
- NYOJ5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- 如何在ABAP Netweaver和CloudFoundry里记录并查看日志
Netweaver 要记录日志需要有一个checkpoint group,可以自行创建也可以使用标准的.这里我重用标准的group:DEMO_CHECKPOINT_GROUP. tcode SAAB, ...
- 提升Web性能的8个技巧总结
提升Web性能的8个技巧总结 在互联网盛行的今天,越来越多的在线用户希望得到安全可靠并且快速的访问体验.针对Web网页过于膨胀以及第三脚本蚕食流量等问题,Radware向网站运营人员提出以下改进建议, ...
- IOS UIView动画(封装动画)
● UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView 将为这些改变提供动画支持 ● 执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视 图, ...
- 【BZOJ2730】[HNOI2012] 矿场搭建(找割点)
点此看题面 大致题意: 一张无向图,要求你在去掉任意一个节点之后,剩余的每个节点都能到达一个救援出口,问至少需要几个救援出口. 第一步:\(Tarjan\)求割点 首先,我们要跑一遍\(Tarjan\ ...
- python_73_pickle序列化(接72)
# json(为字符串形式)用于不同语言之间的数据交互,只适用于简单的数据交互,字典之类可以,函数就不行了,如下例 ''' import json def say(name):print('Hi!', ...
- vue组件-使用插槽分发内容(slot)
slot--使用插槽分发内容(位置.槽口:作用: 占个位置) 官网API: https://cn.vuejs.org/v2/guide/components.html#使用插槽分发内容 使用组件时,有 ...
- Java基础面试题:String 和StringBuffer的区别
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- 关于小程序button控件上下边框的显示和隐藏问题
问题: 小程序的button控件上下有一条淡灰色的边框,在空件上加上了样式 border:(none/0); 都没办法让button上下的的边框隐藏: 代码如下 <button class=&q ...
- 更改yum网易、阿里云的yum源
更改yum源为网易的. 首先备份/etc/yum.repos.d/CentOS-Base.repomv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos ...
- Uva 组装电脑 12124 - Assemble
主要运用二分法查找最优解 #include<iostream> #include<string> #include<vector> #include<map& ...