POJ3461:Oulipo
来源:互联网 发布:巨潮网络资讯 编辑:程序博客网 时间:2024/06/07 02:03
http://poj.org/problem?id=3461
题目很简单,就是寻找给定的模式串在目标串中出现了多少次,采用KMP算法。代码如下:
/*ID: csuchenanAlgorithm: KMPPROG: POJ 3461 OulipoLANG: C++*/#include<iostream>#include<string.h>#include<stdio.h>#include<string>using namespace std ;const int maxn = 10005 ;int next[maxn] ;char patt[maxn] ;string term ;void init() ;void KMP_match() ;int main(){int n ;cin>>n ;cin.ignore() ;while(n--){cin.getline(patt + 1 , sizeof(patt) - 1) ;getline(cin , term) ;memset(next , 0 , sizeof(next)) ;init() ;KMP_match() ;}return 0 ;}void init(){int i ;int k ;int len = strlen(patt + 1) ;next[1] = 0 ;k = 0 ;for(i = 2 ; i <= len ; i ++){while(k > 0 && patt[k + 1] != patt[i])k = next[k] ;if(patt[k+1] == patt[i])k ++ ;next[i] = k ;}}void KMP_match(){int lenp = strlen(patt + 1) ;int lent = term.size() ;int i = 0 ;int q ;int ncount ;ncount = 0 ;q = 0 ;for(i = 0 ; i < lent ; i ++){while(q > 0 && patt[q+1] != term[i] )q = next[q] ;if(patt[q+1] == term[i])q = q + 1 ;if(q == lenp){q = next[q] ;ncount ++ ;}}cout<<ncount<<endl ;}
- POJ3461:Oulipo
- POJ3461 Oulipo
- poj3461 Oulipo
- poj3461-Oulipo
- poj3461 Oulipo
- POJ3461 Oulipo
- POJ3461 Oulipo
- poj3461 Oulipo
- 【POJ3461】Oulipo
- poj3461 Oulipo
- 【POJ3461】Oulipo
- 【KMP】 poj3461 Oulipo
- 【KMP】Oulipo POJ3461
- poj3461 Oulipo (kmp)
- [POJ3461] Oulipo[KMP基础]
- poj3461 Oulipo,Kmp
- POJ3461 Oulipo 【KMP】
- POJ3461 Oulipo KMP
- AreaActivity
- 关于自增操作符的注意事项
- 在Symbian操作系统上开发的手机程序必须进行数字签名,才能在手机终端上安装成功。数字签名机制能够有效阻止恶意手机程序的传播,并保护软件开发商和手机用户的合法权益。那么,如何进行Symbian数字签
- ImageAdapater1
- Qt中图像的显示与基本操作
- POJ3461:Oulipo
- Android开发环境配置
- ImageAdapater2
- 用CSS画各种图形
- 微软面试题:有100万个数字(1到9),其中只有1个数字重复2次,如何快速找出该数字
- Microsoft DirectX 9.0 SDK (December 2004)中没有DirectShow的解决办法
- 致新员工书
- android中的weight
- expect中使用exec执行shell命令的方法