POJ
来源:互联网 发布:淘宝同城网购 编辑:程序博客网 时间:2024/06/11 16:02
点我看题
//这两天看了下KMP,能够去理解要怎么跳,但总感觉还缺点什么东西,只能通过刷一些题目来找找感觉
题意:给一个串P,问在串T中出现了几次。
分析:典型的KMP模板题
参考代码:
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int maxn = 1e6+10;const int maxm = 1e4+10;int n,m;//主串与模式串的长度char T[maxn];//主串char P[maxm];//模式串int nxt[maxm];//前缀表//建前缀表void GetNext(){ int i = 0, j = -1; nxt[0] = -1; while( i < m) { if( j == -1 || P[i] == P[j]) nxt[++i] = ++j; else j = nxt[j]; }}//找出现了多少次int KMPCount(){ if( n == 1 && m == 1) return T[0] == P[0] ? 1 : 0; GetNext(); int ans = 0; int i = 0, j = 0; for( ; i < n; i++) { while( j > 0 && T[i] != P[j]) j = nxt[j]; if( T[i] == P[j]) j++; if( j == m) ans++, j = nxt[j]; } return ans;}int main(){ int T_T; scanf("%d",&T_T); while( T_T--) { scanf("%s",P); scanf("%s",T); n = strlen(T); m = strlen(P); printf("%d\n",KMPCount()); for( int i = 0; i <= m; i++) printf("%d\n",nxt[i]); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Java-11.19
- - The type java.io.ObjectInputStream cannot be resolved. It is indirectly re
- forEach标签的使用
- 矩阵微分
- 机组判断存储方式大小端
- POJ
- 三目运算符详解
- Mybatis中javaType和jdbcType对应关系
- SSM框架搭建
- java使用axis调用webservice接口
- java调用NLPIR(ICTCLAS2016)实现分词功能
- Unity Shader 学习笔记(20) 卷积、卷积核、边缘检测算子、边缘检测
- log日志级别的相关信息
- Golang分布式ID生成服务