HYSBZ 2160 拉拉队排练(回文树)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2160

解题思路:

回文树统计本质不同回文串长度,数目,降序排序后遍历。

快速幂加快乘积运算,然后碰到长度1就不用乘了。

注意本题只要奇数长度回文串。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<set>

using namespace std;

#define ll long long
#define for1(i,a,b) for (int i=a;i<=b;i++)
#define for0(i,a,b) for (int i=a;i<b;i++)
#define rof1(i,a,b) for (int i=a;i>=b;i--)
#define rof0(i,a,b) for (int i=a;i>b;i--)
#define pb push_back
#define fi first
#define se second
#define debug(x) printf("----Line %s----\n",#x)
#define pt(x,y) printf("%s = %d\n",#x,y)
#define INF 0x3f3f3f3f
#define df(x) ll x;scanf("%I64d",&x)
#define df2(x,y) ll x,y;scanf("%I64d %I64d",&x,&y)
#define mod 19930726
#define duozu(T) int T;scanf("%d",&T);while (T--)

const int N = 1e6+5;

const int maxn = 1e6+5;
const int ALP = 26;

char s[N];

ll fast_power(ll a,ll b)
{
    ll ans = 1;
    while (b){
        if (b&1) ans = ans*a%mod;
        b>>=1;
        a = a*a%mod;
    }
    return ans;
}

struct node
{
    int len;
    int cnt;
    bool operator < (const node& a)const{
        return len>a.len;//降序
    }
}str[maxn];

struct PAM{ // 每个节点代表一个回文串
    int next[maxn][ALP]; // next指针,参照Trie树
    int fail[maxn]; // fail失配后缀链接
    int len[maxn]; // 回文串长度
    int s[maxn]; // 存放添加的字符
    int last; //指向上一个字符所在的节点,方便下一次add
    int cnt[maxn];
    int n; // 已添加字符个数
    int p; // 节点个数

    int newnode(int w){
        for(int i=0;i<ALP;i++)
            next[p][i] = 0;
        len[p] = w;
        cnt[p] = 0;
        return p++;
    }
    void init(){
        p = 0;
        newnode(0);
        newnode(-1);
        last = 0;
        n = 0;
        s[n] = -1;
        fail[0] = 1;
    }
    int get_fail(int x){
        while(s[n-len[x]-1] != s[n]) x = fail[x];
        return x;
    }
    void add(int c){
        c -= 'a';
        s[++n] = c;
        int cur = get_fail(last);
        if(!next[cur][c]){
            int now = newnode(len[cur]+2);//len赋值在这
            fail[now] = next[get_fail(fail[cur])][c];
            next[cur][c] = now;
        }
        last = next[cur][c];
        cnt[last]++;
    }
    void count(){
        for (int i=p-1;i>=0;i--)
            cnt[fail[i]] += cnt[i];
    }
}pam;

int main()
{
    int n;
    ll k;//被自己演了
    scanf("%d %lld",&n,&k);
    scanf("%s",s);
    int len = strlen(s);
    pam.init();
    for0(i,0,len) pam.add(s[i]);
    pam.count();

    int tot = 0;
    for1(i,2,pam.p-1){
        str[tot].len = pam.len[i];
        str[tot++].cnt = pam.cnt[i];
    }
    sort(str,str+tot);

    //for0(i,0,tot) printf("len=%d,cnt=%d\n",str[i].len,str[i].cnt);

    ll ans = 1;

    for0(i,0,tot){
        //printf("len=%d,cnt=%d\n",str[i].len,str[i].cnt);
        if (str[i].len&1){
            if (str[i].cnt<k){
                if (str[i].len==1){
                    k-str[i].cnt;
                    continue;
                }
                ans = ans*fast_power((ll)str[i].len,(ll)str[i].cnt)%mod;
                k -= str[i].cnt;
            }
            else {
                if (str[i].len==1){
                    k = 0;
                    break;
                }
                ans = ans*fast_power((ll)str[i].len,k)%mod;
                k = 0;
                break;
            }
        }
    }
    if (k) puts("-1");
    else printf("%lld\n",ans);
    return 0;
}

k不小心定义了int,输入虽然用了longlong但是超出范围后直接暴了,导致TLE,真想把自己头拧下来。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值