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基础 list append 向尾部添加一个元素

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

  2. php编程疑难解决-1

    全局变量和超全局变量 如果是php脚本script 或php代码, 一定要放在 php标签内<?php ?> 内. 这样apache才会把他当做php脚本内容来解析, 才会去调用php模块 ...

  3. Git rebase使用

    目录 rebase的优点和缺点 分支内合并多个commit为一个新commit使用: 命令: 使用: 将其他分支合并到主分支,表现为线性: 将其他分支多个commit合并到主分支,并形成一个新comm ...

  4. win10中命令操作Zookeeper

    目录 zk客户端命令: 连接: 命令: 四字命令: 常用命令: 返回参数说明: 参考: zk客户端命令: 连接: C:\Users\qhong\Desktop $ zkCli.cmd -server ...

  5. Asp.Net 之 DropDownList的使用

    这里不细说,直接上案例 <td style="width: 30px;" align="right"> 年月: </td> <td ...

  6. HIHOcoder 1403 后缀数组一·重复旋律

    思路 后缀数组的板子题,注意后缀数组的rank[]数组是通过位置找到对应排名的,sa[]是通过排名找到位置的,height[i]记录的是sa[i]和sa[i+1]之间的lcp 不要写错了就行了 代码 ...

  7. 题解——Codeforces Round #508 (Div. 2) T2 (构造)

    按照题意构造集合即可 注意无解情况的判断 #include <cstdio> #include <algorithm> #include <cstring> #in ...

  8. communication

    Always consider the challenge as a chance. Basic principles: Know your audience. Know your purpose. ...

  9. 17秋 SDN课程 第一次上机作业

    第一题 拓扑: 测试连通性: 第二题 拓扑: 测试连通性: 第三题 拓扑: 测试连通性:

  10. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...