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. [Swift通天遁地]一、超级工具-(8)地图视图MKMapView的常用代理方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. 清除WebSphere部署应用所对应的JSP缓存

    Web应用部署在WebSphere Application Server v8.5后程序一般放置在<AppServer>/profiles/<profile_name>/ins ...

  3. Visual C++ Windows 桌面应用程序样例(摘抄)

    //================================== //Windows应用程序框架结构(例子) //参考:<Visual C++宝典>陈国建等编著 //======= ...

  4. 406 Queue Reconstruction by Height 根据身高重建队列

    假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...

  5. 为什么,博主我要写下这一系列windows实用网络?

    发现,随着自身一路过来所学,无论在大数据领域.还是linux  or  windows里,菜鸟的我慢慢在长大.把自己比作一个园,面积虽在增加,涉及面增多,但圆外的东西,还是那么多. 现在,正值在校读研 ...

  6. LN : leetcode 3 Longest Substring Without Repeating Characters

    lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...

  7. 从React看weight开发

    从当前云发展的势头来看几乎所有互联网应用都趋向大一统的趋势,一个node下面加一堆应用,同时我们项目也趋向把复杂的大应用拆分成多个小应用,通过各种复杂的Api来协作,通信,达到同样的效果. 可以看出, ...

  8. Appium Python API 汇总(中文版)

    网络搜集而来,留着备用,方便自己也方便他人.感谢总结的人! 1.contexts contexts(self): Returns the contexts within the current ses ...

  9. java设计模式之单例模式总结

    面试手写单例模式(通用版)

  10. 自定义 Java Annotation ,读取注解值

    1. 首先是自定义注解: package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import java.lang ...