60TLE求优化玄关
查看原帖
60TLE求优化玄关
1053122
shy_lihui楼主2024/8/6 17:38
#include<bits/stdc++.h>
using namespace std;
int n,ans;
string s;
stack<char> sta;
int main()
{
    //freopen("game.in","r",stdin);
    //freopen("game.out","w",stdout);
    cin>>n>>s;
    for(int l=0;l<s.size();l++)
    {
        while(!sta.empty())
        {
        	sta.pop();
		}
		int r=(int)s.size()-1;
        if(n>8000)
        {
        	r=min(r,l+500);
		}
        
        for(int i=l;i<=r;i++)
        {
            if(sta.empty())
            {
            	sta.push(s[i]);
			}
            else
            {
                if(sta.top()==s[i])
                {
                	sta.pop();
				}
                    
                else
                {
                	sta.push(s[i]);
				}
                    
            }
            if(sta.empty())
            {
            	ans++;
			}
                
        }
    }
    cout<<ans;
	return 0;
}
2024/8/6 17:38
加载中...