标签:bsp hid opened 反向 targe max a* %s bre
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6740
中文题意链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=872
题解:反向用next数组求循环节(前i个字符的)即可。注意ans初始化应该为-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; }
标签:bsp hid opened 反向 targe max a* %s bre
原文地址:https://www.cnblogs.com/Yokel062/p/11613620.html