HDU 6740 MUV LUV EXTRA(求循环节)
发布时间:2020-12-13 21:26:55 所属栏目:PHP教程 来源:网络整理
导读:题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6740 中文题意链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010cid=872 ? 题解:反向用next数组求循环节(前i个字符的)即可。注意ans初始化应该为-inf(long long) #include
题目链接: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("%lldn",ans); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |