uvalive 4513 Stammering Aliens

时间:2023-11-25 16:25:20

题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标。

思路:hash+LCP+二分答案

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ;
const int x = ;
int n, m, pos;
unsigned long long H[maxn], xp[maxn]; unsigned long long hash[maxn];
int rank[maxn]; int cmp(const int& a, const int& b)
{
return hash[a] < hash[b] || (hash[a] == hash[b] && a < b);
} int possible(int L)
{
int c = ;
pos = -;
for(int i = ; i < n-L+; i++)
{
rank[i] = i;
hash[i] = H[i] - H[i+L]*xp[L];
}
sort(rank, rank+n-L+, cmp);
for(int i = ; i < n-L+; i++)
{
if(i == || hash[rank[i]] != hash[rank[i-]])
c = ;
if(++c >= m)
pos = max(pos, rank[i]);
}
return pos >= ;
} int main()
{
char s[maxn];
while(scanf("%d", &m) == && m)
{
scanf("%s", s);
n = strlen(s);
H[n] = ;
for(int i = n-; i >= ; i--)
H[i] = H[i+]*x + (s[i] - 'a');
xp[] = ;
for(int i = ; i <= n; i++)
xp[i] = xp[i-]*x;
if(!possible())
printf("none\n");
else
{
int L = , R = n+;
while(R - L > )
{
int M = L + (R-L)/;
if(possible(M))
L = M;
else R = M;
}
possible(L);
printf("%d %d\n", L, pos);
}
}
return ;
}