这题是第一次二分+哈希,其实跟一开始想的差不多,没有仔细看数据范围,只要用map出存每种长度的字符串出现的次数就可以了,剩下的就是普通的二分。
当然要注意,注意一些边界条件会不会进去check,否则用flag去判断可能会出错(wa了19次),可以用其他的东西判断
#include<cstdio>
#include<algorithm>
#include<list>
#include<cstring>
#include<iostream>
#include<bitset>
#include<map>
//#define int long long
using namespace std;
int m,len;
char s[40015];
unsigned long long p[40015],ha[40015];
const unsigned long long zhi=131;
bool flag;
bool check(int x)
{
map<unsigned long long,int>mp;
unsigned long long k;
for(int i=1;i+x-1<=len;i++)
{
k=ha[i+x-1]-ha[i-1]*p[x];
if(++mp[k]>=m)
{
//flag=true;
return true;
}
}
return false;
}
int fin(int x)
{
int pos=0;
map<unsigned long long,int>mp;
unsigned long long k;
for(int i=1;i+x-1<=len;i++)
{
k=ha[i+x-1]-ha[i-1]*p[x];
if(++mp[k]>=m)pos=i;
}
return pos-1;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>m)
{
memset(p,0,sizeof(p));
memset(ha,0,sizeof(ha));
flag=false;
if(!m)break;
cin>>s+1;
len=strlen(s+1);
p[0]=1;//ha[0]=0;
for(int i=1;i<=len;i++)
{
ha[i]=ha[i-1]*zhi+(s[i]-'a'+1);
p[i]=p[i-1]*zhi;
}
int l=1,r=len;
while(l<r)
{
int mid=l+r+1>>1;
if(check(mid))
{
l=mid;
}
else
{
r=mid-1;
}
}
if(fin(r)!=-1)
{
cout<<r<<" "<<fin(r)<<endl;
}
else
{
cout<<"none"<<endl;
}
}
}
本文地址:https://blog.csdn.net/qq_37073764/article/details/107385329
如对本文有疑问, 点击进行留言回复!!
牛客编程巅峰赛S1第6场 - 黄金&钻石&王者题解
纵横字谜的答案 Crossword Answers, ACM/ICPC World Finals 1994, UVa232
HDU - 5880 Family View (AC自动机修改母串)
iOS14Beta3续航怎么样 iOS14Beta3续航能力介绍
iOS14Beta3稳定性怎么样 iOS14Beta3升级建议介绍
网友评论