求助一个个人邀请赛题目

学术版

沉石鱼惊旋 @ 2021-12-12 19:46:38

题目

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,ans=0;
    string s;
    cin>>n>>s;
    for(int i=0;i<n;i++)
    {
        int aa=0,bb=0;
        for(int j=i;j<n;j++)
        {
            if(s[j]=='a')
            {
                aa++;
            }
            if(s[j]=='b')
            {
                bb++;
            }   
            if(aa%2==0&&bb%3==0&&abs(aa-bb)%5==0)
            {
                ans+=j-i+1;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

50分代码


by YGB_XU @ 2021-12-12 20:01:54

您这复杂度是n方,肯定会T啊。建议换写法


by 沉石鱼惊旋 @ 2021-12-13 16:11:48

@YGB_XU 鶸的我就是没想到其他写法,只想到了这种O(n^2)的写法


by YGB_XU @ 2021-12-13 17:56:39

建议看这个


|