CCPC2019秦皇岛 J. MUV LUV EXTRA kmp循环节

题目:
在这里插入图片描述
在这里插入图片描述
思路:倒着跑kmp,然后枚举循环起点,求循环节。代入方程求最大值。

#include <bits/stdc++.h>
#define LL long long
using namespace std;

char s[10000010];
int f[10000010];

void get_next(int n){
    int i=0, j=-1;
    f[0]=-1;
    while(i<n){
        if(j==-1||s[i]==s[j]){
            i++, j++, f[i]=j;
        }
        else{
            j=f[j];
        }
    }
}

int main()
{
    LL a, b, tot=-1;
    scanf("%lld%lld%*c", &a, &b);
    char c;
    while(scanf("%c", &c), c!='\n'){
        if(tot!=-1){
            s[tot++]=c;
        }
        if(c=='.'){
            tot=0;
        }
    }
    int L=0, R=tot-1;
    while(L<=R){
        swap(s[L], s[R]);
        L++, R--;
    }
    get_next(tot);
    LL MIN=-1e18;
    for(int i=0; i<tot; i++){
        MIN=max(MIN, a*(i+1)-b*(i+1-f[i+1]));
    }
    printf("%lld\n", MIN);

    return 0;
}

HDU 6740

#include <bits/stdc++.h>
#define LL long long
using namespace std;

char c[10000010];
char s[10000010];
LL f[10000010];

void get_next(LL n){
    LL i=0, j=-1;
    f[0]=-1;
    while(i<n){
        if(j==-1||s[i]==s[j]){
            i++, j++, f[i]=j;
        }
        else{
            j=f[j];
        }
    }
}

int main()
{
    LL a, b, tot=-1;
    while(scanf("%lld%lld%s", &a, &b, c)!=EOF){
        tot=-1;
        int n=strlen(c);
        for(int i=n-1; i>=0&&c[i]!='.'; i--){
            s[++tot]=c[i];
        }
        s[++tot]=0;
        get_next(tot);
        LL MIN=-1e18;
        for(LL i=0; i<tot; i++){

            MIN=max(MIN, a*(i+1)-b*(i+1-f[i+1]));
        }
        printf("%lld\n", MIN);
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值