【LDUOJ】1307 GSS【manacher(回文串算法)】

1307: Problem I. GSS

题目描述

GSS, as the cheerleader, chose n girls for his cheering squad.During one rehearse, N girls lined up in a row, each of whom held a board with one of the 26 lowercase letters written on. GSS found that if for a group of girls of odd number standing in a continuous interval, we could get the exactly same string whether we read the letter on their board from left to right or from right to left, then this group of girls is called harmonious small group (for short, HSG). For example, ”ababa”is a HSG, but “abaa” and”abba”are not. The size of a HSG was defined as the number of girls in the group.GSS wondered the answer to the following question: For all the HSG of this row of girls, if we sort them by descending order of their size, what is the product of the size of the first K HSGs? Since the answer could be very large, you need to print the answer modulo 19930726.


输入

First line contains two integers N and K (N <= 1e6, K <= 1e12), whose meaning are mentioned above.

Second line contains N lowercase letters.


输出

Print a single integer — the answer of the question above modulo 19930726.


样例输入

5 3

ababa


样例输出

45

提示

After sorting, the sequence of harmonious small group is ababa, aba, aba, bab, a, a, a, b, b. The first three harmonious small group is ababa, aba, aba. The product of their length is 5 * 3 * 3 = 45.


【题意】:

给你 n,k,和一个字符串S。

n-字符串的长度,k是第几个,S是字符串。

问题是:给你一个字符串,然后它的所有字串,由奇数个组成的回文串,从大到小排序。

让你输出 ,前k个的长度的乘积,mod(19930726)。


【题解】:

首先,n,k给定数据分别为 :1e6,1e12。

计算回文子串肯定需要算法来实现,不然n^2肯定过不去,还有从k看出来需要用快速幂实现。

以下介绍一种新的回文算法:manacher(马拉车)算法【红书模板】;

void manacher(char str[],ll len[]){
    len[0]=1;
    for ( ll i=1,j=0 ; i<(n<<1) - 1; i++){
        ll p=i>>1,q=i-p,r=((j+1)>>1)+len[j]-1;
        len[i]=r<q?0:min(r-q+1,len[(j<<1)-i]);
        while(p>len[i]-1&&q+len[i]<n&&str[p-len[i]]==str[q+len[i]]){
            ++len[i];
        }if(q+len[i]-1>r){
            j=i;
        }
    }
}

 len[ index ]  指的是:下标为:index ,(注意manacher算法在每一个下标后一位插入了 ‘#’ 来实现 奇数和偶数统一)。

len是该下标为中心的半径长度。如果算回文串长度即(L=len[ index ] *2 -1)


 

 附上代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+10;
const int mod=19930726;
char S[N];
ll a[N]={0};
ll vis[N]={0};
ll len[N]={0};
ll n,k;
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=ans*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return ans%mod;
}
void manacher(char str[],ll len[]){
    len[0]=1;
    for ( ll i=1,j=0 ; i<(n<<1) - 1; i++){
        ll p=i>>1,q=i-p,r=((j+1)>>1)+len[j]-1;
        len[i]=r<q?0:min(r-q+1,len[(j<<1)-i]);
        while(p>len[i]-1&&q+len[i]<n&&str[p-len[i]]==str[q+len[i]]){
            ++len[i];
        }if(q+len[i]-1>r){
            j=i;
        }
    }
}
int main()
{
    while(~scanf("%lld%lld",&n,&k)){
        memset(vis,0,sizeof(vis));
        memset(len,0,sizeof(len));
        memset(a,0,sizeof(a));
        scanf("%s",S);
        manacher(S,len);
        /*for(int i=0;i<n*2;i++){
            printf(" i: %d   %lld\n",i,len[i]);
        }*/
        ll cnt=-1;
        for(ll i=0;i<n*2;i+=2){
            vis[len[i]*2-1]++;
            cnt=max(cnt,len[i]*2-1);
        }
        ll ans=1;
        //printf("cnt : %lld\n",cnt);
        for( ll i = cnt ; i>=1 ; i-=2 ){
            //printf("vis[ %lld ] : %lld\n",i,vis[i]);
            if(k<=vis[i]){
                ans=(ans*qpow(i,k))%mod;
                k-=vis[i];
                vis[i-2]+=vis[i];
                break;
            }else{
                ans=(ans*qpow(i,vis[i]))%mod;
                k-=vis[i];
                vis[i-2]+=vis[i];
            }
        }
        printf("%lld\n",ans%mod);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值