【題解】Codeforces 1307C. Cow and Message

【題目敘述】http://codeforces.com/contest/1307/problem/C

#include <iostream>
using namespace std;
 
string s;
long long mx, ans, cnt;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> s;
    for (int i = 0; i < 26; i++){
        for (int j = 0; j < 26; j++){
            cnt = 0;
            ans = 0;
            for (int k = 0; k < s.length(); k++){
                if (s[k]-'a' == j) ans += cnt;
                if (s[k]-'a' == i) cnt++;
            }
            mx = max(mx, ans);
        }
        mx = max(mx, cnt);
    }
    cout << mx << "\n";
}
分享本文 Share with friends