POJ3461Oulipo 题解
题目大意:
求字符串A在字符串B中出现的次数。
思路:
KMP板题,用Hash也可水过~要学习KMP可参考http://blog.csdn.net/u011564456/article/details/20862555
代码:
KMP:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; char s1[],s2[];
int next[]; int main()
{
int n,i,l1,l2,j,ans;
scanf("%d",&n);
while (n--)
{
scanf("%s%s",s1,s2);
l1=strlen(s1),l2=strlen(s2);
for(j=next[]=,i=;i<l1;i++)
{
for (;j && s1[j]^s1[i];j=next[j-]);
if (s1[i]==s1[j]) j++;
next[i]=j;
}
for (i=j=ans=;j<l2;j++)
{
for (;i && s1[i]^s2[j];i=next[i-]);
if (s1[i]==s2[j]) i++;
if (i==l1) ans++,i=next[i-];
}
printf("%d\n",ans);
}
return ;
}
Hash:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int S=,mod=,N=,M=;
char s1[N],s2[M];
long long Hash1[N],Hash2[M],mi[M]; int gethash(int l,int r)
{
return (Hash2[r]-Hash2[l-]*mi[r-l+]%mod+mod)%mod;
} int main()
{
int n,i,j,l1,l2,ans;
for (mi[]=i=;i<=M;i++) mi[i]=mi[i-]*S%mod;
scanf("%d",&n);
while (n--)
{
scanf("%s%s",s1+,s2+);
l1=strlen(s1+),l2=strlen(s2+);
for (i=;i<=l1;i++) Hash1[i]=(Hash1[i-]+s1[i])*S%mod;
for (i=;i<=l2;i++) Hash2[i]=(Hash2[i-]+s2[i])*S%mod;
for (ans=,i=;i<=l2-l1+;i++)
if (gethash(i,i+l1-)==Hash1[l1]) ans++;
printf("%d\n",ans);
}
return ;
}
POJ3461Oulipo 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- jQuery和JS原生方法对比
- jq点击和鼠标移上效果以及一个等号"=" 二个等号"==" 三个等号"===" 的区别
<body> <div class="a" bs='1' style="width:100px; height:30px; border:1px sol ...
- Android Studio 生成Jar包时遇到的gradlew下载问题
网上介绍说使用gradlew打包jar,可是输入gradlew makeJar后就开始download XXX.zip,但是等了很久都没有完成.解决办法如下: 原文:http://blog.csdn ...
- 数据结构和算法 – 8.链表
8.1.数组存在的问题 在处理列表的时候数组是常用的数据结构.数组可以对所存储的数据项提供快速地存取访问,而且它很易于进行循环遍历操作.当然,数组已经是语言的一部分了,用户不需要使用额外的内存,也不需 ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 为什么是 n(n+1)/2 ?
n(n+1)/2是一个数列的元素两两运算后的不重复结果数.如图: 假如数列a = 1,2,3....n.那么该数列内的元素两两相乘,则会构建出上图中的表格,这个表格应该有n x n 个元素. 用程序写 ...
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- 【J2EE入门】13个规范
看过了j2ee教学视频,真的让我痛彻心扉,那叫痛并快乐着,痛是因为看了这么长时间,自己只知道了13个规范的概念:同样,快乐也正是因为我知道了13个规范的概念,接下来就是逐项实践的学习. 看了教学视频, ...
- autoprefixer
自动化补全工具,在写兼容的css样式的时候,自动补全-webkit,-moz等 sublime和websotrm上都可以安装此工具.
- -webkit-text-size-adjust
ios使用-webkit-text-size-adjust禁止调整字体大小 body{-webkit-text-size-adjust: 100%!important;} android使用以下代码, ...