Cow and Message

C - Cow and Message

对于字符串问题,如果对于下标进行操作复杂度很大的话,可以考虑对字母进行操作,这样计算复杂度的时候就是对26进行计算了。

// Created by CAD on 2020/3/3.
#include <bits/stdc++.h>
#define ll long long
using namespace std;

queue<int> q[30];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;cin>>s;
    int len=s.length();
    for(int i=0; i<len; ++i){
        q[s[i]-'a'+1].push(i);
    }
    ll ans=0;
    for(int i=1;i<=26;++i){
        ans=max(ans,(ll)q[i].size());
        for(int j=1;j<=26;++j){
            queue<int> a=q[i],b=q[j];
            ll sum=0;
            while(!a.empty()){
                while(!b.empty()&&b.front()<=a.front()) b.pop();
                sum+=b.size();
                a.pop();
            }
            ans=max(ans,sum);
        }
    }
    cout<<ans<<"\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CADCADCAD/p/12401431.html
今日推荐