【CF1063F】String Journey 哈希

题目大意

  给你一个字符串 \(s\),让你找出最大的 \(k\),满足:能从 \(s\) 中选出 \(k\) 个不重叠的字符串 \(t_1,t_2,\ldots,t_k\),且 \(\forall i,\lvert t_i\rvert >\lvert t_{i+1}\rvert\)\(t_{i+1}\)\(t_i\) 的子串,\(t_{i+1}\) 的出现位置在 \(t_i\) 后面。

  \(n\leq 500000\)

题解

  显然最优方案中 \(t_{i+1}\) 就是把 \(t_i\) 的第一个字符或最后一个字符删掉得到的。

  记 \(f_i\) 为从 \(i\) 开始的后缀,选一个前缀作为 \(t_1\) 所能得到的最大的 \(k\)

  那么可以二分 \(f_i\),然后判断 \(s_{i\sim i+f_i-1}\) 删掉前缀/后缀之后得到的字符串在后面任意一次出现的位置的 \(f\) 值是否 \(\geq f_i-1\)

  这样是 \(O(n\log^2 n)\) 的。

  注意到答案 \(\leq O(\sqrt n)\),那么就可以枚举每个长度 \(\leq 1000\) 的字符串,然后用哈希判断。

  时间复杂度:\(O(n\sqrt n)\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<functional>
#include<cmath>
#include<vector>
#include<iostream>
//using namespace std;
using std::min;
using std::max;
using std::swap;
using std::sort;
using std::reverse;
using std::random_shuffle;
using std::lower_bound;
using std::upper_bound;
using std::unique;
using std::vector;
using std::cin;
using std::cout;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
    char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
void open2(const char *s){
#ifdef DEBUG
    char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
int rd(){int s=0,c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;}
void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');}
int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
typedef long double ldb;
ll n,k;
ll l,r;
ll ans=-1;
ll len;
int main()
{
    open("d");
    cin>>n>>l>>r>>k;
    len=(r>=l?r-l+1:n-l+1+r);
    if(n<=1000000)
    {
        for(int i=n;i<=2*n;i++)
        {
            ll s=k%i;
            if(s==0)
                s=i;
            s-=len;
            ll mi=max(i-n-(n-len)-1,0ll);
            ll ma=min(len,i-n);
            if(s>=mi&&s<=ma)
                ans=max(ans,i-n);
        }
    }
    else
    {
        if(k>=len&&k<=2*len)
            ans=max(ans,k==2*len?n:n-len+k-len+1);
        for(int i=1;i<=k/n;i++)
        {
            ll r=min((k-2*len+2*n+1)/(i+1),(k-len)/i);
            ll l=max((k-2*len+i-1)/i,(k-len+n+1+i)/(i+1));
            if(l<=r)
                ans=max(ans,r-n);
        }
    }
    cout<<ans<<std::endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ywwyww/p/9789607.html
今日推荐