codeforces 1307C Cow and Message (dp)
题目链接 : https://codeforces.com/problemset/problem/1307/C
看起来等差数列这个条件非常难做,但仔细分析后发现,任何长度大于 2 的序列,都可以拆分成长度为 1 或 2的序列
于是从前向后扫,考虑每个位置的字母对答案的贡献
- 对长度为 1 的序列的贡献:该字母出现次数 +1
- 对长度为 2 的序列的贡献:26 字母与该字母组合成长度为 2 的序列,贡献就是组合的字母在前面出现的次数
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 100010;
int n;
ll dp1[26],dp2[26][26];
char s[maxn];
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
scanf("%s",s);
n = strlen(s);
for(int i=0;i<n;++i){
int a = s[i] - 'a';
for(int j=0;j<26;++j){
dp2[j][a] += dp1[j];
}
++dp1[a];
}
ll ans = 0;
for(int i=0;i<26;++i){
ans = max(ans,dp1[i]);
for(int j=0;j<26;++j){
ans = max(ans,dp2[i][j]);
}
}
printf("%lld\n",ans);
return 0;
}