zoukankan      html  css  js  c++  java
  • 【洛谷P1659】啦啦队排练

    题目大意:给定一个长度为 N 的字符串,求前 K 个长度为奇数的回文子串的长度的乘积是多少。

    题解:利用回文自动机,将所有长度的回文串和个数求出来,按照长度排序进行模拟即可。

    代码如下

    // luogu-judger-enable-o2
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+10;
    const int mod=19930726;
    typedef long long LL;
    LL fpow(LL a,LL b){
        LL ret=1%mod;
        for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;
        return ret;
    }
    
    char s[maxn];
    int n; LL k;
    struct node{int len,sz;}t[maxn];
    struct PAM{
        int lst,tot,trie[maxn][26],fail[maxn],len[maxn],sz[maxn];
        PAM(){tot=1,fail[0]=fail[1]=1,len[1]=-1;}
        void extend(int c,int i){
            int p=lst;
            while(s[i-len[p]-1]!=s[i])p=fail[p];
            if(!trie[p][c]){
                int now=++tot,k=fail[p];
                while(s[i-len[k]-1]!=s[i])k=fail[k];
                fail[now]=trie[k][c],len[now]=len[p]+2;
                trie[p][c]=now;
            }
            lst=trie[p][c],++sz[lst];
        }
        void solve(){
            for(int i=tot;i>=2;i--){
                t[i].sz=sz[i],t[i].len=len[i];
                sz[fail[i]]+=sz[i];
            }
            sort(t+2,t+tot+1,[](const node &x,const node &y){
                return x.len>y.len;
            });
        }
    }pam;
    
    int main(){
        scanf("%d%lld%s",&n,&k,s+1);
        for(int i=1;i<=n;i++)pam.extend(s[i]-'a',i);
        pam.solve();
        int now=2,r=pam.tot,ans=1;
        while(k){
            if(now>r)return puts("-1"),0;
            if(t[now].len%2==0){++now;continue;}
            if(t[now].sz<=k)ans=ans*fpow(t[now].len,t[now].sz)%mod,k-=t[now].sz;
            else ans=ans*fpow(t[now].len,k)%mod,k=0;
            ++now;
        }
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    Vim的行号、语法显示等设置,即.vimrc文件的配置
    Vim 配置详解
    经典SQL语句大全
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    GHOST XP SP3无法安装IIS服务的解决方法
    开发中巧用Enum枚举类型
    代码的坏味道
    C#转义字符
    Dijit、ExtJS、jQuery UI 异同浅析
    未雨绸缪 Plan to Throw One Away
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10788459.html
Copyright © 2011-2022 走看看