codeforce div1+2 621 C.cow message

Here Insert Picture DescriptionStandard output cow Bessie Farmer John had just intercepted a message sent to Burger King! However, Bessie convinced that hidden inside a secret message. Text is a string of lower-case Latin letters. She thought to hide the string in the string S, T, if T is present as a sequence S, the index S configuration arithmetic progression. For example, the string in the string aaabb aab hidden because it appears at index 1, 3 and 5, which form an arithmetic progression indexes which difference is public 2. Bessie think most of any occurrences of the string are hiding secret messages. If the index set are different, the two occurrences of the sequence S is different. To help her find out the number of occurrences of this secret message! For example, in the string aa abb, a is hidden 3 times, b hide 2, ab 6 hidden, the hidden three AA, bb hide 1, 2 hidden AAB, 1 AAA hide, abb hide 1, AAAB hidden 1, aabb hide 1, aaabb hide once. Number of occurrences of confidential information 6. Input of the first line contains a Latin small letter string s (1≤ | s | ≤105) - Bercy taken text. Output a single integer - the number of times the confidential message.

This question seems to deal with relatively many cases, but after observing not difficult to find all the answers length greater than 2 or less equal to a certain answer 1 or 2, so we only need to consider a string that is one or two lengths can, then let's build a storage array that so far the number of characters that appear at each position, combined for 26 of its previous character using 2-dimensional array combined cycle, the maximum real-time updates, you can to all possible strings of length two, and comparing the number of occurrences of a single string before the end of each cycle, the answer is to ensure that the maximum length or 21.

#include <bits/stdc++.h>
using namespace std;
long long b[500][500],c[500],ans=0;
string s;
main(){
    cin>>s;
    for(int i=0;i<s.size();i++){
        for(int q ='a';q <='z';q++){
            b[q][s[i]]+=c[q];
            ans=max(ans,b[q][s[i]]);}
        c[s[i]]++;
        ans=max(ans,c[s[i]]);}
    cout << ans<<endl;}
Published 48 original articles · won praise 17 · views 4459

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104375517