hdu-4080 Stammering Aliens 字符串hash 模板题
http://acm.hdu.edu.cn/showproblem.php?pid=4080
求出现次数大于等于n的最长串。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include <string>
#include <map>
#define LL long long
using namespace std;
const LL p=1e9+;
const LL mod=1e9+;//23333 951413 11111111111111111111111
const LL N=;
string s;
LL n,st;
LL has[N];//计算前缀has
LL xp[N];//计算p的次方值,用于还原区间hash
LL fastMi(LL a,LL b){
LL k,mut=;
if(b==) return ;
k=a*a;
if(b%!=) mut*=a;
mut*=fastMi(k,b/);
return mut;
}
int idx(char x)
{
return x-'a'+;
}
void ini_has()
{
xp[]=;
for(int i=;i<N;i++)
xp[i]=xp[i-]*p,xp[i]%=mod;
}
void m_has(string &s)
{
has[]=;
for(int i=;i<=s.length();i++)
has[i]=(has[i-]*p+idx(s[i-]))%mod;
}
LL g_has(int l,int r)//[l+1,r]
{
return ((has[r]-has[l]*xp[r-l])%mod+mod)%mod;
}
struct node
{
int c;
int s;
node(int cc,int ss)
{
c=cc;
s=ss;
}
node(){
}
};
bool ok(int len)
{
//cout<<len<<endl;
map<int,node> mp;
for(int i=;i+len<=s.length();i++)
{
LL h=g_has(i,i+len);
if(mp.find(h)==mp.end())
mp[h]=node(,i);
else
{
mp[h].c++;
mp[h].s=i;
}
}
bool f=false;
st=-;
for(map<int,node>::iterator it=mp.begin();it!=mp.end();it++)
{
if(it->second.c>=n)
{
f=true;
st=max(st,(LL)it->second.s);
}
}
return f;
}
int bins(int l,int r)
{
while(r-l>=)
{
int mid=(l+r)/;
if(ok(mid))l=mid;
else r=mid-;
}
for(int i=r;i>=l;i--)
if(ok(i))return i;
return ;
}
int main()
{
cin.sync_with_stdio(false);
ini_has();
while(cin>>n)
{
if(!n)break;
cin>>s;
has[]=;
m_has(s);
int ans=bins(,s.length());
if(ans)
{
cout<<ans<<' '<<st<<endl;
}else{ cout<<"none"<<endl;}
}
return ;
}
hdu-4080 Stammering Aliens 字符串hash 模板题的更多相关文章
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- HDU 1880 字符串hash 入门题
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
- HDU 1874 畅通工程续(模板题——Floyd算法)
题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...
- HDU 1301-Jungle Roads【Kruscal】模板题
题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
随机推荐
- CXF整合spring,在tomcat中发布webService
服务端 1.首先下载CXF的jar包 http://pan.baidu.com/s/1dFBwSRf 密码: qyax.里面自带了需要用到的spring的jar包. 或者使用maven,如下配置.不论 ...
- MongDB 数据库使用技巧及注意事项
说明 本文记录MongoDB数据库的使用技巧总结,特性发现. 利用TTL特性实现数据定时删除 限制: 你不能创建TTL索引,如果要索引的字段已经在其他索引中使用. 索引不能包含多个字段. 索引的字段必 ...
- Linux rsync 命令学习
Rsync命令和cp命令很像,但是功能似乎更加复杂点,主要用来备份数据.看了网上一堆介绍的文章,感觉不是很通俗易懂.下面按照我的理解,做一些笔记: 同步方式 之前接触过一些同步软件,例如坚果云.百度云 ...
- tp剩余未验证内容
new Image(宽度,高度) $(image).attr('src', ...).load(function(){....}) load表示浏览器从服务器下载(装载)对象完成, 这个load方法很 ...
- CF113D 高斯消元、dp
题目链接 https://codeforces.com/contest/113/problem/D 思路 \(k[i]=\frac{1-p[i]}{ru[i]}\) f[i][j]表示经过i和j的次数 ...
- loj SDOI2017数字表格
目录 太才了 注意,错误 代码 太才了 \(\prod \limits_{i=1}^{n}\prod\limits_{j=1}^{m}f[gcd(i,j)]\) \(\prod\limits_{k=1 ...
- Trimmomatic过滤Illumina低质量序列
1. 下载安装 直接去官网下载二进制软件,解压后的trimmomatic-0.36.jar即为我们需要的软件 官网: http://www.usadellab.org/cms/index.php?pa ...
- JS获取节点的兄弟,父级,子级元素
https://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作 ...
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
https://www.cnblogs.com/zhbsh/archive/2013/04/22/3035477.html 用C#.NET调用Java开发的WebService时,先在客户端封装的带有 ...
- jvm:垃圾收集器
垃圾收集器: Serial 收集器: 单线程收集器,专注做收集,会暂停别的工作.收集效果好. ParNew 收集器: 是Serial的多线程版本.目前只有它能和CMS收集器配合. Paralle ...