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.
样例输入
  1. 3
  2. 11
  3. 1001110110
  4. 101
  5. 110010010010001
  6. 1010
  7. 110100010101011
样例输出
  1. 3
  2. 0
  3. 3
  4.  
  5. 此题第一感觉就是用KMP算法,代码如下
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. char a[];
  5. char b[];
  6. int next[];
  7.  
  8. void getNext() {
  9. next[] = -;
  10. int len = strlen(a);
  11. int i = ,j = -;
  12. while(i < len) {
  13. if(j == - || a[i] == a[j]) {
  14. i++,j++;
  15. next[i] = j;
  16. }
  17. else {
  18. j = next[j];
  19. }
  20. }
  21. }
  22.  
  23. int calCnt() {
  24. int at = ;
  25. int bt = ;
  26. int ans = ;
  27. int lena = strlen(a);
  28. int lenb = strlen(b);
  29. while(bt < lenb) {
  30. if(at == - || a[at] == b[bt]) {
  31. at++;
  32. bt++;
  33. if(at == lena) {
  34. ans++;
  35. at = next[lena];
  36. }
  37. continue;
  38. }
  39. if(a[at] != b[bt]) {
  40. at = next[at];
  41. }
  42.  
  43. }
  44. return ans;
  45. }
  46.  
  47. int main(int argc, char const *argv[])
  48. {
  49. int n;
  50. while(scanf("%d",&n) != EOF) {
  51. while(n--) {
  52. scanf("%s",a);
  53. scanf("%s",b);
  54. getNext();
  55. int ans = calCnt();
  56. printf("%d\n", ans);
  57. }
  58. }
  59. return ;
  60. }

此算法第一要求出next数组。而求next数组的过程本身也是一个自己和自己匹配的过程。此处用i不断前进,j不断返回,用作匹配。

计数时是新的匹配过程。和求next数组的过程神似。

若求nextval数组可能会更快些,代码如下

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. char a[];
  5. char b[];
  6. int nextval[];
  7.  
  8. void getnextval() {
  9. nextval[] = -;
  10. int len = strlen(a);
  11. int i = ,j = -;
  12. while(i < len) {
  13. if(j == - || a[i] == a[j]) {
  14. i++,j++;
  15. if(i < len && a[i] == a[j]) {
  16. nextval[i] = nextval[j];
  17. }
  18. else {
  19. nextval[i] = j;
  20. }
  21.  
  22. }
  23. else {
  24. j = nextval[j];
  25. }
  26. }
  27. }
  28.  
  29. int calCnt() {
  30. int at = ;
  31. int bt = ;
  32. int ans = ;
  33. int lena = strlen(a);
  34. int lenb = strlen(b);
  35. while(bt < lenb) {
  36. if(at == - || a[at] == b[bt]) {
  37. at++;
  38. bt++;
  39. if(at == lena) {
  40. ans++;
  41. at = nextval[lena];
  42. }
  43. continue;
  44. }
  45. if(a[at] != b[bt]) {
  46. at = nextval[at];
  47. }
  48.  
  49. }
  50. return ans;
  51. }
  52.  
  53. int main(int argc, char const *argv[])
  54. {
  55. int n;
  56. while(scanf("%d",&n) != EOF) {
  57. while(n--) {
  58. scanf("%s",a);
  59. scanf("%s",b);
  60. getnextval();
  61. int ans = calCnt();
  62. printf("%d\n", ans);
  63. }
  64. }
  65. return ;
  66. }

要注意15行的条件。但实际运行好像并没有更快。

今天偶然发现nyoj可以查看优秀的代码,这一点简直完爆其他oj,看到这样一种非常取巧的办法

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(int argc, char const *argv[])
  6. {
  7. string s1,s2;
  8. int n;
  9. cin >> n;
  10.  
  11. while(n--) {
  12. cin >> s1;
  13. cin >> s2;
  14. size_t m = s2.find(s1,);
  15. int ans = ;
  16. while(m != string::npos) {
  17. ans++;
  18. m = s2.find(s1,m+);
  19. }
  20. cout << ans << endl;
  21. }
  22.  
  23. return ;
  24. }

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. OpenLayers学习笔记2——坐标转换问题

    参照别人的添加marker的demo来改造时,发现无论怎样更改经纬度,都是停留在同一个位置.过了一两天突然想起可能是坐标参考的问题,尝试搜了一下,果然是这个问题.问题是这样子的: WMTS中地图的坐标 ...

  2. Feign + Hystrix 服务熔断和服务降级

    本机IP为  192.168.1.102 1.    新建 Maven 项目   feign 2.   pom.xml <project xmlns="http://maven.apa ...

  3. C# 界面跳转-登陆之后跳转至主窗口

    在登陆按钮验证成功之后可以将会话结果改为OK //验证通过之后将对话结果设置为OK(之后会载入主界面) this.DialogResult = DialogResult.OK; this.Dispos ...

  4. Bzoj 1081 [Ahoi2009] chess 中国象棋

    bzoj 1081 [Ahoi2009] chess 中国象棋 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1801 状态比较难设,的确 ...

  5. 用户和用户组以及 Linux 权限管理

    1.从 /etc/passwd 说起 前面的基本命令学习中,我们介绍了使用 passwd 命令可以修改用户密码.对于操作系统来说,用户名和密码是存放在哪里的呢?我们都知道一个站点的用户名和密码是存放在 ...

  6. h5获取摄像头拍照功能

    完整代码展示 <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <met ...

  7. 【PHP】根据两地经纬度计算距离

    最近做一个H5活动的项目,有个要求是必须现场玩家才能参与,所以就需要计算玩家位置和活动地点的距离来判断是否在活动现场. 以下是写的一个根据经纬度计算两地距离的方法 1 function getDist ...

  8. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  9. day 85 Vue学习七之vue-cookie

      Vue学习七之vue-cookie   通过vue如何操作cookie呢 参考链接:https://www.jianshu.com/p/535b53989b39 第一步:安装vue-cookies ...

  10. python正则表达式入门篇

    文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...