C. Cow and Message

时间:2020-05-26
本文章向大家介绍C. Cow and Message,主要包括C. Cow and Message使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.

The text is a string ss of lowercase Latin letters. She considers a string tt as hidden in string ss if tt exists as a subsequence of ss whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices 11, 33, and 55, which form an arithmetic progression with a common difference of 22. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of SS are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!

For example, in the string aaabb, a is hidden 33 times, b is hidden 22 times, ab is hidden 66 times, aa is hidden 33 times, bb is hidden 11 time, aab is hidden 22 times, aaa is hidden 11 time, abb is hidden 11 time, aaab is hidden 11 time, aabb is hidden 11 time, and aaabb is hidden 11 time. The number of occurrences of the secret message is 66.

Input

The first line contains a string ss of lowercase Latin letters (1|s|1051≤|s|≤105) — the text that Bessie intercepted.

Output

Output a single integer  — the number of occurrences of the secret message.

Examples
input
Copy
aaabb
output
Copy
6
input
Copy
usaco
output
Copy
1
input
Copy
lol
output
Copy
2
Note

In the first example, these are all the hidden strings and their indice sets:

  • a occurs at (1)(1), (2)(2), (3)(3)
  • b occurs at (4)(4), (5)(5)
  • ab occurs at (1,4)(1,4), (1,5)(1,5), (2,4)(2,4), (2,5)(2,5), (3,4)(3,4), (3,5)(3,5)
  • aa occurs at (1,2)(1,2), (1,3)(1,3), (2,3)(2,3)
  • bb occurs at (4,5)(4,5)
  • aab occurs at (1,3,5)(1,3,5), (2,3,4)(2,3,4)
  • aaa occurs at (1,2,3)(1,2,3)
  • abb occurs at (3,4,5)(3,4,5)
  • aaab occurs at (1,2,3,4)(1,2,3,4)
  • aabb occurs at (2,3,4,5)(2,3,4,5)
  • aaabb occurs at (1,2,3,4,5)(1,2,3,4,5)
Note that all the sets of indices are arithmetic progressions.

In the second example, no hidden string occurs more than once.

In the third example, the hidden string is the letter l.

不知道怎么证明的两或一个字符的时候最大,与顺序有关的一般用前缀和或后缀和统计

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const int mod = 998244353;
const int N = 2e5 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
ll a2[26][26],a1[26];

int main()
{
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        int c = s[i] - 'a';
        for (int j = 0; j < 26; j++)
        {
            a2[j][c] += a1[j];
        }
        a1[c]++;
    }
    ll ans = 0;
    for (int i = 0; i < 26; i++)
    {
        ans = max(ans, a1[i]);
    }
    for (int i = 0; i < 26; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            ans = max(ans, a2[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/dealer/p/12963381.html