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 ...
随机推荐
- WordPress企业建站心得
回头聊聊我用WordPress做企业网站的事.说是企业网站,其实就是一个小的企业展示网站.事情要从我爸开了一家自行车店开始说起,自从他开了自行车店,不但开始学着玩起了微信(因为要做微信营销),又想到了 ...
- POJ 1631 Bridging signals(LIS的等价表述)
把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...
- Linux 文件的压缩与解压
1. tar结尾压缩命令 [root@test ~]# tar -cvf grub.tar /boot/grub/ 查看压缩包文件 [root@test ~]# tar -vtf grub.tar ...
- python_7_while
count=0 while True: print('count:',count) count+=1 # count=count+1 if count==500: break#结束整个循环
- gulp的常用插件
gulp和webpack的差别:https://www.cnblogs.com/lovesong/p/6413546.html var gulp = require('gulp'); var del ...
- 虚拟机Linux_Mint中安装vmtools增强工具
一开始用VmwarePro安装Linux系统时,系统的整体界面会缩在屏幕中间的一小块区域内.如图: 看的会非常吃力.为了更好的解决这个问题,就需要安装Vmtools增强工具.安装步骤如下: 1. ...
- django+xadmin在线教育平台(六)
4-1 使用py3.6和django1.11开发系统前注意事项 直接通过Python3.6和django最新版本来开发我们的系统的一些注意事项. 原版本: Python 2.7 & djang ...
- 一、Shell 教程
Shell 教程 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个 ...
- phpstudy配置SSL证书的步骤(Apache环境)以及一些注意事项
准备工具(我自己的): 腾讯云的域名和云主机,还有SSL证书,以及phpstudy 首先要下载自己的SSL证书,会得到一个压缩包,解压以后会得到四个文件夹和一个csr文件, Apache文件夹内三个文 ...
- 静态属性property的本质和应用
一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...