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 & ...
随机推荐
- java 中的一个项目如何做到访问另一个项目的一个方法 或者 页面
两种方法:1.将一个项目打成jar包,第二个项目进行导入该jar包,就可以使用第一个项目里的类方法属性等2.将第一个项目发布出去,然后第二个项目调用,所谓发布出去就是开发远程接口,允许其他人调用.
- 【SQL】检索满足条件的最大值的数据集合
是不是看题目觉的看不懂?其实我自己也看不懂,但是又找不到更好的词来形容. 为了更好的表达我的意思,请看下. 如果有一张成绩表(Points), 学生(student) 成绩(point) 科目(sub ...
- Delphi字符串的基本操作与常用函数
参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- ytu 2558: 游起来吧!超妹!(水题,趣味数学题)
2558: 游起来吧!超妹! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3[Submit][Status][Web Board ...
- HTML5的input color系统颜色选择器
前两天,我写了一篇<推荐两款jQuery色盘选择器>介绍,那是使用JavaScript实现的色盘,今天我给大家介绍HTML5的色盘选择器.HTML5有一个input类型为color,即颜色 ...
- POJ3415 Common Substrings(后缀数组 单调栈)
借用罗穗骞论文中的讲解: 计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来.先将两个字符串连起来,中间用一个没有出现过的字符隔开.按height ...
- sqlplus 中spool命令的简单用法
spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...
- Bat脚本实现MySQL数据库SQL文件备份
@echo offecho 在线兑奖系统自动备份脚本(请勿关闭) 联系人: 电话::loopset /a "FDate=%date:~,4%%date:~5,2%%date:~8,2%&q ...
- wp8 入门到精通 数据库更新字段(一)
public class UserInfoDB : BaseDB { public UserInfoDB() : base(@"Data Source=isostore:\MakeLove\ ...