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 模板题的更多相关文章

  1. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  2. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  3. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  4. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  5. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  6. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  7. HDU 1874 畅通工程续(模板题——Floyd算法)

    题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...

  8. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...

  9. 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)

    思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...

随机推荐

  1. Python3基础 dict setdefault 根据键查找值,找不到键会添加

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. bzoj 3437 小p的农场

    bzoj 3437 小p的农场 思路 \(f[i]=min(f[j]+\sum\limits_{k=j+1}^{i}{b[k]*(i-k)}+a[i])\) \(f[i]=min(f[j]+\sum\ ...

  3. 最小二乘法拟合非线性函数及其Matlab/Excel 实现

    1.最小二乘原理 Matlab直接实现最小二乘法的示例: close x = 1:1:100; a = -1.5; b = -10; y = a*log(x)+b; yrand = y + 0.5*r ...

  4. [转] Java中的final、static、this、super

    final 关键字 final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便 ...

  5. orcl 之 导入和导出(2)

    删除用户表,表数据等内容 drop user username cascade 无法为表空间 spancename 中的段创建INITAL区处理办法 找到表空间存储的地方文件 select file_ ...

  6. kubernetes 实战4_命令_Configure Pods and Containers

    Configure Service Accounts for Pods A service account provides an identity for processes that run in ...

  7. Dispose in c#

    在标准的Dispose模式中,真正的IDisposable接口的Dispose方法并没有做实际的清理工作,它其实是调用了下面的这个带bool参数且受保护的的虚方法: protected virtual ...

  8. 洛谷P1244 青蛙过河 DP/思路

    又是一道奇奇怪怪的DP(其实是思路题). 原文戳>>https://www.luogu.org/problem/show?pid=1244<< 这题的意思给的挺模糊,需要一定的 ...

  9. 使用 node 创建代码服务器

    var express = require('express'); var proxy = require('http-proxy-middleware'); var app = express(); ...

  10. js实现类型jq的dom加载完成

    有时候我们只想在 dom 加载完成后运行 js ,而不是等所有图片加载完成.所以不需要 onload , onload 会加载图片等其他媒体.很消耗时间. 原:http://blog.csdn.net ...