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. vue中父组件传数据给子组件

    父组件: <template> <parent> <child :list="list"></child> //在这里绑定list对 ...

  2. npm更换为淘宝镜像源

    1.通过config命令   1 2 npm config set registry http://registry.cnpmjs.org npm info underscore (如果上面配置正确这 ...

  3. C头文件中尖括号与双引号的区别及编译搜索顺序

    这两天被问到一个很有意思的问题:C头文件中尖括号与双引号有什么区别,以前只大约知道 <> 常用在系统库文件,"" 常用在自定义的借口文件中,那具体在gcc编译搜索过程中 ...

  4. Java调用ssl异常(javax.net.ssl.SSLHandshakeException: No appropriate protocol)

    今天做升级用了jdk1.8发现java调用SSL的时候,突然一下抛出一个异常 经过一阵瞎搞,最后才发现是因为jdk1.8版本导致SSL调用权限上有问题. 解决办法:找到jdk 1.8安装目录,找到C: ...

  5. WKWebView 和 UIWebView 允许背景音乐自动播放(记录)

    WKWebView WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.allowsInlin ...

  6. HDU 4135 容斥原理

    思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cas ...

  7. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  8. connection failed to http://nssa-sensor3:11000/oozie/?user.name=oozie(<urlopen erroer Errno 111] Connection refused>)解决办法(图文详解)

    不多说,直接上干货! 解决办法 Copy/Paste oozie.services property tag set from oozie-default.xml to oozie-site.xml. ...

  9. C语言指针的理解以及指针的指针的理解

    指针指向的是内存地址编号,内存地址编号指向的是对应的内容. 我们需要一个变量,来储存内存地址编号,这个变量的值是一个内存地址编号,但是我们可以通过修改变量的值,来不断的改变内存地址编号. 但是,我们如 ...

  10. Hive DDL&DML

    1.删除分区 ALTER TABLE table_name DROP IF EXISTS PARTITION(dt=') 如果是外部表,记得rm对应文件 2.添加分区 ALTER TABLE tabl ...