zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Acwing第 68 场周赛【未完结】

AcWing 周赛 68
2023-09-11 14:15:52 时间

https://www.acwing.com/activity/content/competition/problem_list/2274/

4612. 去掉0【双指针】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int t; cin>>t;
    while(t--)
    {
        string s; cin>>s;
        vector<int>ve;
        int ans=0;
        for(int i=0;i<s.size();i++)
        {
            if( (s[i]=='1') && (i+1<s.size()) && (s[i+1]=='0') )
            {
                int j=i+1;
                while( (j+1)<s.size() && (s[j+1]=='0') ) j++;
                if(j+1<s.size()&&s[j+1]=='1') ans+=j-i;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

4613. 方格跳跃【前缀和】

在这里插入图片描述
前缀和,左右分别扫一遍即可。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5*3+10;
int st[N];
int main(void)
{
    int n; cin>>n;
    string s; cin>>s;
    s=")"+s;
    for(int i=n;i>=1;i--)
    {
        if(s[i]=='<') st[i]=1;
        st[i]=max(st[i],st[i+1]);
    }
    int cnt=0;
    for(int i=1;i<=n;i++) if(s[i]=='>'&&!st[i+1]) cnt++;
    memset(st,0,sizeof st);
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='>')  st[i]=1;
        st[i]=max(st[i],st[i-1]);
    }
    for(int i=1;i<=n;i++) if(s[i]=='<'&&!st[i-1]) cnt++;
    cout<<cnt;
    return 0;
}