[省选前题目整理][POJ 3461]Oulipo(KMP)
来源:互联网 发布:安卓数独源码 编辑:程序博客网 时间:2024/05/17 18:01
题目链接
http://poj.org/problem?id=3461
题目大意
给出模式串W和主串T,求W在T中的出现次数。
思路
最基础的KMP。
代码
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#define MAXN 1001000#define MAXM 10010using namespace std;char word[MAXM],text[MAXN];int next[MAXM];void getnext(char str[],int len){ int k=0; next[1]=0; for(int i=2;i<=len;i++) { while(k>0&&str[k+1]!=str[i]) k=next[k]; if(str[k+1]==str[i]) k++; next[i]=k; }}int match(char W[],int lenW,char T[],int lenT){ int ans=0; getnext(W,lenW); int k=0; //W已经匹配的长度 for(int i=1;i<=lenT;i++) { while(k>0&&W[k+1]!=T[i]) k=next[k]; if(W[k+1]==T[i]) k++; if(k==lenW) { ans++; k=next[k]; } } return ans;}int main(){ int T; scanf("%d",&T); while(T--) { memset(next,0,sizeof(next)); scanf("%s",word+1); scanf("%s",text+1); printf("%d\n",match(word,strlen(word+1),text,strlen(text+1))); } return 0;}
0 0
- [省选前题目整理][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
- Oulipo - POJ 3461 KMP
- 【KMP】 POJ 3461 Oulipo
- KMP---POJ 3461Oulipo
- POJ 3461 Oulipo (KMP)
- poj 3461 Oulipo kmp
- 【KMP】POJ-3461 Oulipo
- KMP POJ-3461 Oulipo
- poj-3461 Oulipo 【kmp】
- PHP 验证登陆类
- (R语言)起步
- 第五周项目二之对象作为数据成员
- sencha延迟加载速度(1)
- 狂省两行代码
- [省选前题目整理][POJ 3461]Oulipo(KMP)
- Android 高仿 QQ5.0 侧滑菜单效果 自定义控件来袭
- EMV技术学习和研究-脱机数据认证之DDA
- 公钥和私钥
- 算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS
- vsftpd服务器的安装与详解
- sencha延迟加载速度(2)
- 第五周项目:体验常成员函数
- (R语言)简单操作,数值与向量