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
- 样例输出
-
- 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 ;
- }
- 3
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 ...
随机推荐
- OpenLayers学习笔记2——坐标转换问题
参照别人的添加marker的demo来改造时,发现无论怎样更改经纬度,都是停留在同一个位置.过了一两天突然想起可能是坐标参考的问题,尝试搜了一下,果然是这个问题.问题是这样子的: WMTS中地图的坐标 ...
- Feign + Hystrix 服务熔断和服务降级
本机IP为 192.168.1.102 1. 新建 Maven 项目 feign 2. pom.xml <project xmlns="http://maven.apa ...
- C# 界面跳转-登陆之后跳转至主窗口
在登陆按钮验证成功之后可以将会话结果改为OK //验证通过之后将对话结果设置为OK(之后会载入主界面) this.DialogResult = DialogResult.OK; this.Dispos ...
- Bzoj 1081 [Ahoi2009] chess 中国象棋
bzoj 1081 [Ahoi2009] chess 中国象棋 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1801 状态比较难设,的确 ...
- 用户和用户组以及 Linux 权限管理
1.从 /etc/passwd 说起 前面的基本命令学习中,我们介绍了使用 passwd 命令可以修改用户密码.对于操作系统来说,用户名和密码是存放在哪里的呢?我们都知道一个站点的用户名和密码是存放在 ...
- h5获取摄像头拍照功能
完整代码展示 <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <met ...
- 【PHP】根据两地经纬度计算距离
最近做一个H5活动的项目,有个要求是必须现场玩家才能参与,所以就需要计算玩家位置和活动地点的距离来判断是否在活动现场. 以下是写的一个根据经纬度计算两地距离的方法 1 function getDist ...
- 科学计算库Numpy——运算
np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...
- day 85 Vue学习七之vue-cookie
Vue学习七之vue-cookie 通过vue如何操作cookie呢 参考链接:https://www.jianshu.com/p/535b53989b39 第一步:安装vue-cookies ...
- python正则表达式入门篇
文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...