HDU 4821 2013长春现场赛hash
题意:
一个字符串S 问其中有几个子串能满足以下条件:
1、长度为M*L
2、可以被分成M个L长的小串 每个串都不一样
分析:
hash方法,一个种子base,打表出nbase[i]表示base的i次方
将以i位字符开头之后的串hash成一个无符号长整型:hash[i]=hash[i+1]*base+str[i]-'a'+1
然后每个L长度的小串的hash值即为:hash[i]-hash[i+L]*nbase[L]
map记录hash值的个数
以i位字符开头的字符串与以i+L位字符开头的字符串只相差两个地方,减去hash[i ~ i+L],加上hash[i+M*L ~ i+(M+1)*L]
这样算节省了很多时间
#include <bits/stdc++.h>
using namespace std; typedef unsigned long long ull;
const int maxn=100005;
char str[maxn];
ull shash[maxn],nbase[maxn],base=31;
map<ull,int>mp; int main()
{
// freopen("in.txt","r",stdin);
int M,L;
nbase[0]=1;
for(int i=1;i<maxn;i++)
nbase[i]=nbase[i-1]*base;
while(~scanf("%d%d",&M,&L))
{
scanf("%s",str);
int n=strlen(str);
shash[n]=0;
for(int i=n-1;i>=0;i--)
shash[i]=shash[i+1]*base+str[i]-'a'+1;
int ans=0;
for(int i=0;i<L && i+M*L<=n;i++)
{
mp.clear();
for(int j=i;j<i+M*L;j+=L)
mp[shash[j]-shash[j+L]*nbase[L]]++;
if(mp.size()==M) ans++;
for(int j=i+L;j+M*L<=n;j+=L)
{
ull tmp=shash[j-L]-shash[j]*nbase[L];
mp[tmp]--;
if(mp[tmp]==0) mp.erase(tmp);
mp[shash[j+(M-1)*L]-shash[j+M*L]*nbase[L]]++;
if(mp.size()==M) ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
HDU 4821 2013长春现场赛hash的更多相关文章
- HDU 4816 Bathysphere (2013长春现场赛D题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 2013长春区域赛的D题. 很简单的几何题,就是给了一条折线. 然后一个矩形窗去截取一部分,求最 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- HDU 4818 Golden Radio Base (2013长春现场赛B题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 进制转换. 现场根据题目给的两个公式,不断更新!!! 胡搞就可以了. 现场3A,我艹,一次循环开 ...
- HDU 4815 Little Tiger vs. Deep Monkey(2013长春现场赛C题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 简单的DP题. #include <stdio.h> #include <st ...
- HDU 4815 Little Tiger vs. Deep Monkey 2013 长春现场赛C题
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 [题意] n个题目,每题有各自的分数,A有50%的概率答对一道题目得到相应分数,B想要在至少P的概率 ...
- hdu 4813(2013长春现场赛A题)
把一个字符串分成N个字符串 每个字符串长度为m Sample Input12 5 // n mklmbbileay Sample Outputklmbbileay # include <iost ...
- hdu 4764 && 2013长春网赛题解
一个组合游戏题. 解答: 从后面往前面推,首先n-1是必胜位,然后前面的k位是必败位,如此循环下去.所以题目就容易了! 代码: #include<cstdio> using namespa ...
- hdu 4788 (2013成都现场赛 H题)
100MB=10^5KB=10^8B 100MB=100*2^10KB=100*2^20B Sample Input2100[MB]1[B] Sample OutputCase #1: 4.63%Ca ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- NFS PersistentVolume(11)
一.部署nfs服务端 1.需在 k8s-master 节点上搭建了一个 NFS 服务器,目录为 /nfsdata: yum install -y nfs-utils rpcbind vim /etc/ ...
- 019.Python函数sorted,filter和推导式
一 sorted函数 sorted(iterable,reverse=False,key=函数) 功能:排序 参数: iterable:可迭代性数据(常用:容器类型数据,range对象,迭代器) re ...
- DOCKER学习_011:使用Dockerfile制作docker镜像
前面使用commit的方式,制作一个docker镜像,本次介绍使用Dockerfile制作一个dockers镜像 [root@docker-server3 ~]# mkdir /openssh [ro ...
- Day029 JDK8中新日期和时间API (四)
JDK8中新日期和时间API 其他的一些API ZoneId:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris ZonedDateTime:一个在ISO-8601日历系统时区的 ...
- Splunk 8.2.0 发布 (macOS, Linux, Windows)
强烈鄙视 CSDN 用户 CIAS(账号:hanzheng260561728),盗用本站资源,删除原文链接,并且用于收费下载!!! 请访问原文链接:https://sysin.org/article/ ...
- rman备份出现ORA-19625
[oracle@hear adump]$ rman target / Recovery Manager: Release 11.2.0.4.0 - Production on Mon Jun 17 0 ...
- Java读取SQL server数据库
要打开SQL server 的三个服务,然后再执行代码. package com.sql; import java.sql.SQLException; import java.sql.Statemen ...
- Mybatis基础使用方法
1.首先在数据库中建立一张表 create table login( name varchar(20) not null, username varchar(20) not null, passwor ...
- 理解 this
this this 取什么值是在函数执行的时候确认的,不是在函数定义的时候确认的 this 的不同应用场景,this 的指向 函数在调用时,js 会默认给 this 绑定一个值,this 的值与绑定方 ...
- python_reques接口测试框架,Excel作为案例数据源
一.框架菜单 1.1 common模块 1.2 其他 二.Excel接口测试案例编写 三.读取Excel测试封装(核心封装) excel_utils.py 读取Excel中的数据 import o ...