poj_3461: Oulipo
基础KMP题,本文提供一段能AC并且便于调试以及查看next数组的代码。
参考博客
http://blog.csdn.net/v_july_v/article/details/7041827
http://www.cnblogs.com/kuangbin/archive/2012/08/14/2638803.html
#include<iostream>
#include<cstring>
using namespace std;
;
int next[N];
char S[N],T[N]; //T为模式串,S为主串
int slen,tlen;
// 第一步先学习写好这个函数
int KMP_Index()
{
,j=;
while(i<slen&&j<tlen)
{
||S[i]==T[j])
i++,j++;
else
j=next[j];
}
if(j==tlen)
return i-tlen+1; //标号从0开始
else
;
}
// 第二步学习写好这个函数
int KMP_Count()
{
;
;
&&tlen==)
]==T[];
;i<slen;i++)
{
&&S[i]!=T[j])
j=next[j];
if(S[i]==T[j])
j++;
if(j==tlen)
{
ret++;
j=next[j];
}
}
return ret;
}
// 第三步,本着满足前两个函数的需求这一目标,来学习写好这个函数
// 而这一步,正是KMP算法精华所在
void getNext()
{
int j,k;
j=;k=-;next[]=-;
while(j<tlen)
||T[j]==T[k])
next[++j]=++k;
else
k=next[k];
}
void printNext()
{
;i<tlen;i++)
printf("%3c",T[i]);
puts("");
;i<tlen;i++)
printf("%3d",next[i]);
puts("");
}
int main()
{
// while(cin>>T)
// tlen=strlen(T),getNext(),printNext();
int tt;cin>>tt;
while(tt--)
{
cin>>T>>S;
slen=strlen(S);
tlen=strlen(T);
getNext();
printNext();
cout<<KMP_Count()<<endl;
// for(int i=0;i<tlen;i++)
// printf("%d",next[i]);
// puts("");
// cout<<"模式串T在主串S中首次出现的位置是: "<<KMP_Index()<<endl;
// cout<<"模式串T在主串S中出现的次数为: "<<KMP_Count()<<endl;
}
}
poj_3461: Oulipo的更多相关文章
- C++之路进阶——poj3461(Oulipo)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35694 Accepted: 14424 Descript ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- Match:Oulipo(POJ 3461)
Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...
- KMP算法 hdu4686 Oulipo
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- hdu----1686 Oulipo (ac自动机)
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- Oulipo (kmp)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26857 Accepted: 10709 Descript ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
随机推荐
- mongo查询系统
首先,我们先向集合(collections)中添加测试文档(documents).如下: > for(i=1;i<=5;i++) db.test.insert({x:i,y:i*i,z:6 ...
- 38. Count and Say - Unsolved
https://leetcode.com/problems/count-and-say/#/description The count-and-say sequence is the sequence ...
- python之numpy库[1]
python-numpy python中的数据 一维数据 用列表和集合表示 数组与列表的关系 列表:数据类型可以不同 数组:数据类型可以相同 多维数据 用列表表示 高维数据 用字典表示 高维数据仅利用 ...
- python之numpy库[2]
python-numpy csv文件的写入和存取 写入csv文件 CSV (Comma‐Separated Value, 逗号分隔值),是一种常见的文件格式,用来存储批量数据. 写入csv文件 np. ...
- 【论文:麦克风阵列增强】An Algorithm For Linearly Constrained Adaptive Array Processing
作者:桂. 时间:2017-06-03 15:06:37 链接:http://www.cnblogs.com/xingshansi/p/6937635.html 原文链接:http://pan.ba ...
- canvas学习总结三:绘制路径-线段
Canvas绘图环境中有些属于立即绘制图形方法,有些绘图方法是基于路径的. 立即绘制图形方法仅有两个strokeRect(),fillRect(),虽然strokezText(),fillText() ...
- flask 扩展之 -- flask-login
一. 使用 Werkzeug 实现密码散列. generate_password_hash(password, method=pbkdf2:sha1, salt_length=8) 将原始密码作为输入 ...
- php隔行换色输出表格
<?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...
- django-xadmin列表页filter关联对象搜索问题
环境:xadmin-for-python3 python3.5.2 django1.9.12 问题描述:Product ProductSku两个实体,ProductSku FK外键关联Product ...
- Codeforces 818B Permutation Game
首先看一下题目 B. Permutation Game time limit per test 1 second memory limit per test 256 megabytes input s ...