while(scanf("%s",str+)== ) {
        int n=strlen(str+);
        next[]=; int j=;
        for(int i=;i<=n;i++) {
            while(j&&str[j+]!=str[i]) j=next[j];
            if(str[i]==str[j+]) j++;
            next[i]=j;
        }
        for(int i=;i<=n;i++) printf("%d ",next[i]);

随便给出一个字符串  对应的next数组为

A B R A C D A B R A

0 0 0 1 0 1 0 2 3 4

其实就是找前缀了拉

模式匹配代码贴一份。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <vector>
 6 #include <list>
 7 #include <queue>
 8 using namespace std;
 9 const int MAX = 1e6+;
 const int inf = 0x3f3f3f3f;
 char str1[MAX],str2[MAX];
 int next[MAX];
 int main()
 {
     //freopen("in","r",stdin);
     //freopen("out","w",stdout);
     int cas;
     scanf("%d",&cas);
     while(cas--) {
         scanf("%s %s",str1+,str2+);
         int n=strlen(str1+);
         next[]=; int j=;
         for(int i=;i<=n;i++) {
             if(j&&str1[i]!=str1[j+]) j=next[j];
             if(str1[i]==str1[j+]) j++;
             next[i]=j;
         }
         int ans=;
         int len=strlen(str2+); j=;
         for(int i=;i<=len;i++) {
             while(j&&str2[i]!=str1[j+]) j=next[j];
             if(str2[i]==str1[j+]) j++;
             if(j==n) ans++;
 
         }
         printf("%d\n",ans);
 
     }
 
     return ;
 }

KMP超强模板贴一份的更多相关文章

  1. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  2. KMP(模板)

    算法讲解: KMP算法最浅显易懂 模板来源: 从头到尾彻底理解KMP 首先:KMP的模板为: void get_next(char *a, int *nex) { nex[] = ; , j = ; ...

  3. KMP算法模板&&扩展

    很不错的学习链接:https://blog.csdn.net/v_july_v/article/details/7041827 具体思路就看上面的链接就行了,这里只放几个常用的模板 问题描述: 给出字 ...

  4. POJ3461 【KMP(粗糙模板)】

    题意: 给你两个字符串p和s,求出p在s中出现的次数. 这道题,abababa中aba出现了3次. 有其他题是求abababa,aba就是2次. 需注意. KMP 模板 //#include<b ...

  5. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  6. KMP(模板)

    kmp算法是解决单模匹配问题的算法,难点在于求next[]数组 求next[]数组:对于模板串的所有前缀子串的最长公共前后缀的长度,就是next[]数组的值 eg:主串为cbbbaababac  子串 ...

  7. Kmp 算法模板 C

    /** * name:KMP * time:2012-11-22 * 字符串快速匹配 */ #include<stdio.h> #include<string.h> typed ...

  8. KMP算法模板

    不懂的话推荐看这篇博客,讲的很清楚 http://blog.csdn.net/v_july_v/article/details/7041827 #include<iostream> #in ...

  9. KMP算法———模板

    做出KMP字符串匹配算法心情也是好好哒,萌萌哒. 感谢黄学长,感谢栋栋! #include<cstdio>#include<string>#include<iostrea ...

随机推荐

  1. bzoj1528 sam-Toy Cars(贪心,优先队列)

    「BZOJ1528」[POI2005] sam – Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Ja ...

  2. AMD的规范使用

    1.解决命名冲突 2.解决繁琐的文件依赖 3. 可读性.可依赖性 参考这里 // foobar.js // 私有变量 var   test = 123: //  公有方法 function  foot ...

  3. OpenGL 2D模式

    // // left top 这里设置的默认是左上角 // void push_view2d(int left, int top, int width, int height) { //glPushA ...

  4. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  5. Jayway JsonPath实例

    开源:https://github.com/json-path/JsonPath 引入库: <dependency> <groupId>com.jayway.jsonpath& ...

  6. CentOS 7下ElasticSearch集群搭建案例

    最近在网上看到很多ElasticSearch集群的搭建方法,本人在这人使用Elasticsearch5.0.1版本,介绍如何搭建ElasticSearch集群并安装head插件和其他插件安装方法. 一 ...

  7. Android 滚动RecyclerView加载图片时的流畅度优化

    实现:使用onScrollStateChanged回调检测滚动状态,并在RecyclerViewAdapter内部设置类似isScrolling的状态值来控制网络图片的加载. 下面是代码举例: // ...

  8. Python 语言搭建SELENIUM测试环境,搭建过程记录。

    第一步,安装Python: 第二步,安装SetupTools: 第三步,安装Pip: 第四步,安装selenium(for python) 第五步,新建第一个基于Firefox的测试用例 上述 只是步 ...

  9. Tornado引入静态css、js文件

    一.静态路径 template_path=os.path.join(os.path.dirname(__file__), "templates") 这里是设置了模板的路径,放置模板 ...

  10. 【C++】智能指针简述(四):shared_ptr

    在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...