POJ 3461 (kmp)
来源:互联网 发布:手机淘宝旧版5.2.2 编辑:程序博客网 时间:2024/06/11 02:12
http://poj.org/problem?id=3461
题目大意:
给s1和s2 问s1在s2中出现多少次
分析:
kmp模板题吧 只有真正理解才能做的出来的
AC代码:
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include<list>#include <bitset>#include <climits>#include <algorithm>#define gcd(a,b) __gcd(a,b)#define mset(a,n) memset(a,n,sizeof(a))#define FINfreopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)typedef long long LL;const LL mod=1e9+7;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);using namespace std;int next[1000010];char str1[1000010];char str2[1000010];int sum;void get_next(char *str){ int n=strlen(str); int j=-1; next[0]=-1; for (int i=1;i<n;i++){ while (j>=0&&str[i]!=str[j+1]) j=next[j]; if (str[i]==str[j+1]) j++; next[i]=j; }}void KMP(char *str2,char *str1){ get_next(str2); int len1=strlen(str1); int len2=strlen(str2); int j=-1; for (int i=0;i<len1;i++){ while (j>=0&&str1[i]!=str2[j+1]) j=next[j]; if (str1[i]==str2[j+1]) j++; if (j==len2-1) sum++,j=next[j]; }}int main (){ int t; // FIN; scanf ("%d",&t); while (t--){ scanf ("%s%s",str2,str1); sum=0; KMP(str2,str1); printf ("%d\n",sum); } return 0;}
阅读全文
0 0
- POJ 3461 (KMP)
- POJ 3461 (kmp)
- POJ 3461 - Oulipo(KMP)
- POJ-3461 Oulipo(KMP)
- Poj 3461 Oulipo (KMP)
- POJ---3461-Oulipo(KMP)
- POJ 3461 Oulipo(KMP)
- POJ 3461 Oulipo(KMP)
- 【POJ 3461】Oulipo(kmp)
- poj 3461 Oulipo(KMP)
- poj 3461 Oulipo(KMP)
- poj 3461:Oulipo(KMP模版)
- POJ 3461 Oulipo(简单KMP)
- poj 3461 Oulipo(kmp 模板题)
- Poj 3461 Oulipo(KMP算法)
- POJ 3461 Oulipo(KMP模板)
- POJ 3461 Oulipo (KMP算法)
- kmp算法(POJ 3461 Oulipo)
- 实验一 Linux操作系统的安装
- 日记—9.21
- 基本类型转换规则和sizeof的用法
- opencv学习笔记—8,阈值操作
- 欢迎使用CSDN-markdown编辑器
- POJ 3461 (kmp)
- 9.21(周四)
- jQuery——对象样式操作
- Spring第一个案例
- DS总结篇--三种快排及四种快排优化实现
- python:pandas(6),用法及函数总结
- 行保持说明符@和@@用法详解
- JSON格式
- VMware安装kali liunx2.0实现与物理机共享文件及剪切板