首页 > 其他分享 >G1. Division + LCP (easy version)

G1. Division + LCP (easy version)

时间:2024-05-04 20:25:18浏览次数:19  
标签:Division 200005 G1 int mid version easy

原题链接

题解

1.二分查找前缀出现次数,用 \(kmp\) 优化查找算法

code

#include<bits/stdc++.h>
using namespace std;
char s[200005];
int pre[200005]={0},occ[200005]={0};
int n,x;
int solve(int len)
{
    int cnt=1;
    int it=0;
    for(int j=len+1;j<=n;j++)
    {
        while(it&&s[it+1]!=s[j]) it=pre[it];
        if(s[it+1]==s[j]) it++;
        if(it==len)
        {
            cnt++;
            it=0;
        }
    }
    return cnt;
}

void init()
{
        pre[1]=0;
        for(int i=2;i<=n;i++)
        {
            int it=pre[i-1];
            while(it)
            {
                if(s[it+1]==s[i]) break;
                it=pre[it];
            }
            if(s[it+1]==s[i]) pre[i]=it+1;
            else pre[i]=0;
        }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>x>>x;
        cin>>(s+1);

        init();

        int l=0,r=n+1;
        while(l+1<r)
        {
            int mid=(l+r)/2;
            if(solve(mid)>=x) l=mid;
            else r=mid;
        }

        cout<<l<<"\n";
    }
    return 0;
}

标签:Division,200005,G1,int,mid,version,easy
From: https://www.cnblogs.com/pure4knowledge/p/18172627

相关文章

  • [oeasy]python0015_键盘改造_将esc和capslock对调_hjkl_移动_双手正位
    键盘改造......
  • G2. Division + LCP (hard version)
    G2.Division+LCP(hardversion)Thisisthehardversionoftheproblem.Inthisversion$l\ler$.Youaregivenastring$s$.Forafixed$k$,consideradivisionof$s$intoexactly$k$continuoussubstrings$w_1,\dots,w_k$.Let$f_k$bethemaximal......
  • D2. Reverse Card (Hard Version)
    D2.ReverseCard(HardVersion)Thetwoversionsaredifferentproblems.Youmaywanttoreadbothversions.Youcanmakehacksonlyifbothversionsaresolved.Youaregiventwopositiveintegers$n$,$m$.Calculatethenumberoforderedpairs$(a,b)$......
  • You have an error in your SQL syntax; check the manual that corresponds to your
    在进行增加User对象时采用了PreparedStatement对象,方法executeQuery()和executeUpdate()都是无参方法,所以不要写成prepareStatement.executeQuery(sql)或prepareStatement.executeUpdate(sql)publicintadd(Useruser){Connectionconnection=JDBCTools.getConnecti......
  • 简单解决version 'GLIBC_2.34' not found,version 'GLIBC_2.25' not found
    简单解决version'GLIBC_2.34'notfound,version'GLIBC_2.25'notfound无需手动下载安装包编译前言很多博客都是要手动下载安装包进行编译升级,但这样很容易导致系统崩溃,本博文提供一个简单的方法,参考自博客1,博客2.检查版本strings/usr/lib64/libc.so.6|grepGLIBC_或者......
  • 报错“Please indicate a valid Swagger or OpenAPI version field”
    报错“PleaseindicateavalidSwaggerorOpenAPIversionfield”报错信息PleaseindicateavalidSwaggerorOpenAPIversionfield.Supportedversionfieldsareswagger:"2.0"andthosethatmatchopenapi:3.0.n(forexample,openapi:3.0.0). 原因分析根......
  • CF1967B2 Reverse Card (Hard Version) 题解
    题意:求有多少对\((a,b)\)满足\(b\times\gcd(a,b)\equiv0\pmod{a+b},1\lea\len,1\leb\lem\)。首先我们设\(\gcd(a,b)=G,a=i\timesG,b=j\timesG\),显然有\(\gcd(i,j)=1\)。那么可以把原条件转化为\(j\timesG\)是\((i+j)\)的倍数。因为\(\gcd(i+......
  • Reverse Card (Hard Version)
    事情是这样的,我验了这一场CF。显然我玩原神玩多了有一个很奇怪的、不能过的算法,哦,当然,在我本机可以过。为了展现自己的智慧糖,我写一下。出题人是先发给我了一个限制都是\(n\)的,因此只有这个。\(n,m\)改改就是了。要求\(1\lea\len,1\leb\len\)满足\(a+b\midb\times......
  • java EasyExcel 导出不同dto到多sheet,同时有动态字段,分页写入方案,解决存在oom的问题
    思路 1将一次查询数据改成分页查询,比如一次2000条,2将每次查询的数据按业务分组计算每类业务动态列追加的最大次数treeMap追加列2在excel列表头则是追加2列,名称自定义,我这边是补数字,示例追加列1,追加列2我的业务是按数据库存放的图片来确定最大追加列,需要将图片......
  • 使用 EasyExcel 进行数据解析
    一、添加pom.xml导入相关依赖<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.12</version></dependency><depende......