HDU 6740 MUV LUV EXTRA (Section circulated refrigerant)

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=6740

Chinese meaning of the questions Links: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=872

 

Solution: Reverse circulation section with next array seek to (i Front characters). Note ans initialization should -inf (long long)

#include <bits/stdc++.h>

const int maxn=1e7+5;
int a, b;
char s[maxn], x[maxn];
int next[maxn];

void calnext(char x[], int m)
{
    int i=0, j=-1;
    next[0]=-1;
    while(i<m)
    {
        while(j!=-1 && x[i]!=x[j]) j=next[j];
        next[++i]=++j;
    }
}


int main()
{
    while(~scanf("%d%d%s", &a, &b, s))
    {
        int cnt=0;
        for(int i=strlen(s)-1; i>=0; i--)
        {
            if(s[i]=='.') break;
            x[cnt++]=s[i];
        }
        calnext(x, cnt);
        long long ans=-1e18;
        for(int i=1; i<=cnt; i++)
            ans=std::max(ans, 1ll*a*i-1ll*b*(i-next[i]));
        printf("%lld\n", ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Yokel062/p/11613620.html