poj 3461Oulipo
题目链接:http://poj.org/problem?id=3461
统计字符串出现的次数
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
#include<queue>
#include<iterator>
#include<vector>
#include<set>
#define dinf 0x3f3f3f3f
typedef long long ll;
//const int Max=(1<<16)+10;
using namespace std;
#define SIZE 100000005 const int N = ;
int m_next[N];
char S[N],T[N];
int slen, tlen; void getNext()
{
int j, k;
j = ; k = -; m_next[] = -;
while(j < tlen)
if(k == - || T[j] == T[k])
m_next[++j] = ++k;
else
k = m_next[k]; }
/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
int i = , j = ;
getNext(); while(i < slen && j < tlen)
{
if(j == - || S[i] == T[j])
{
i++; j++;
}
else
j = m_next[j];
}
if(j == tlen)
return i - tlen+;
else
return -;
}
/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
int ans = ;
int i, j = ; if(slen == && tlen == )
{
if(S[] == T[])
return ;
else
return ;
}
getNext();
for(i = ; i < slen; i++)
{
while(j > && S[i] != T[j])
j = m_next[j];
if(S[i] == T[j])
j++;
if(j == tlen)
{
ans++;
j = m_next[j];
}
}
return ans;
}
int main()
{ int TT;
int i, cc;
string str;
cin>>TT;
while(TT--)
{
getchar(); scanf("%s %s",&T,&S);
slen = strlen(S);
tlen = strlen(T);
printf("%d\n",KMP_Count()); //cout<<"模式串T在主串S中首次出现的位置是: "<<KMP_Index()/2+1<<endl;
//cout<<"模式串T在主串S中出现的次数为: "<<KMP_Count()<<endl;
}
return ;
}
poj 3461Oulipo的更多相关文章
- KMP算法 学习例题 POJ 3461Oulipo
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37971 Accepted: 15286 Description The ...
- POJ 3461Oulipo KMP模板
KMP之所以线性,因为匹配的时候i是不往回走的 我们只用调整j的位置 假设在s中找t 用二元组(i,j)表示s串的[i-j+1,i] 与t串的[1,j]匹配 假设s[i+1]和t[j]匹配上了,就j+ ...
- POJ:3461-Oulipo(KMP模板题)
原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
随机推荐
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...
- PHP学习路线
0x1 0x2
- [翻译]opengl扩展教程1
[翻译]opengl扩展教程1 原文地址https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/extensions.php [翻译]ope ...
- 再谈Weiphp公众平台开发——1、增加插件
去年开始接触基于Weiphp的公众平台开发,一直没时间好好整理一下. 下面开始讲解第一个自定义weiphp插件:MyHello的开发流程. 1.插件创建.在weiphp管理后台依次点击“插件管理-&g ...
- 5.5---整数A转成整数B(CC150)
自己的: public static int calcCost(int A,int B){ int ans = 1; int temp = A ^ B; while(temp != 1){ if(te ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- LINQ查询数组里面是否包含某值
#region linq to 数组 //定义数组,并初始化 string [] array = new string []{"Juan" ...
- poj 3126
一道搜索的水题,其实搜索相对其他的来说好掌握一点,因为有个固定的模板可以用去套 题目大意就是数字的变化,一个数字只可以变化到它最相邻的一个素数上去,意思是只变化一位数字,求最短的变化方案 #inclu ...
- CentOS卸载OpenJDK并安装Sun JDK
第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...
- spring + myBatis 常见错误:SQL语法错误
在程序运行时,有时候会出现如下错误: 这个错误通常是你的sqlmapper.xml中sql语句语法有错误.所以请仔细查看你sql语句是否正确,比如{#id}这样写就会报上述错误,其实应该#{id}这样 ...