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 ...
随机推荐
- Install jdk on Ubuntu16
wikiHow to Install Oracle Java JDK on Ubuntu Linux This tutorial will cover the installation of 32-b ...
- 当模版引擎遇到点("."),会按照下列顺序查询:
字典查询,例如:foo["bar"] 属性或方法查询,例如:foo.bar 数字索引查询,例如:foo[bar]
- uniGUI试用笔记(十一)
最近研究了一下UniGUI的TuniDBGrid,记录一下免得忘记了. TuniDBGrid的重要属性包括: 1.列—TUniDBGridColumns和TUniDBGridColumn 每个列对象( ...
- Docker 入门指南——部署常用服务示例
MongoDB FROM centos:centos7 MAINTAINER The CentOS Project <cloud-ops@centos.org> RUN yum -y up ...
- ibus-libpinyin 无法选择除第一个外的候选词
其实不只一个人遇到这问题 https://github.com/libpinyin/ibus-libpinyin/issues/127 临时可用的解决办法是: 清理libpinyin的cache目录相 ...
- lamp服务器被人恶意绑定域名的解决办法
还没开始就被别人绑定了域名 事情的起因与发现 刚买了个服务器搭建了一个dz,想着域名还没备案,就先搭建了起来,然后在做DDOS测试时偶然发现服务器被别人恶意把域名绑定了 最初的解决方案 没管..... ...
- OpenCV学习一《Linux下安装OpenCV》
第一步:安装源码前先安装好需要的第三⽅方环境 需要的编译环境■ [compiler] sudo apt-get install build-essential # 注释说明 64位ubuntu在安装 ...
- vscode "没有活动的源代码控制提供程序" 解决办法
确保安装git F1,输入Show built-in Extensions, 左侧列表中找Git,然后启用即可
- Python学习 day01打卡
1.Python : 是一门解释型 弱类型 高级开发编程语言. 2.第一个Python程序的编写: print ("hell,world") 3.变量:把程序运行过程中的值储存起来 ...
- CSS sprites
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式. 优点: 它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一 ...