Codeforces1307C. Cow and Message

读题,我们知道选1个、选2个的时候是没有冲突的,大于2个的时候就有限制,那么我们观察是否可以把大于2个的情况都转换一下,可以发现,如果有一个串的长度大于2,且出现的次数大于2,那么,组成这个串里必定有一个长度为2的串,出现的次数大于2,那我们就可以把所有大于2的长度转换为长度为2的,这样题目转换成长度为1与长度为2的最大值,复杂度就是O(26*|s|),一般字符串dp都要考虑26个字母

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;

LL dp1[26], dp2[26][26];

void run_case() {
    string str;
    cin >> str;
    for(int i = 0; i < str.size(); ++i) {
        int now = str[i] - 'a';
        for(int j = 0; j < 26; ++j)
            dp2[j][now] += dp1[j];
        dp1[now]++;
    }
    LL ans = 1;
    for(int i = 0; i < 26; ++i) ans = max(ans, dp1[i]);
    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
            ans = max(ans, dp2[i][j]);
    cout << ans;
}
 
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    //cout.setf(ios_base::showpoint);cout.precision(10);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}
View Code

 

posted @ 2020-02-18 21:50  GRedComeT  阅读(251)  评论(0编辑  收藏  举报