首页 > 其他分享 >USACO 2023 January Contest, Bronze Problem 3. Moo Operations

USACO 2023 January Contest, Bronze Problem 3. Moo Operations

时间:2023-02-06 20:26:10浏览次数:49  
标签:Operations return Contest int January Moo ans Bronze

 

 

 

 这道题目灰常简单,我们先从最简单的3个字符串开始

有以下几种情况:

 

 可以看到,只有在中间是O的情况下才有可能变成MOO

辣么我们不妨在在s串中枚举这个中间 O

每枚举到一个就看看能不能用他的本身操作次数加上删除旁边的字符次数得出的实际操作次数来更新ans(取min)(记得在for开始前判断s长度是否<3,<3就return -1)

最后如果ans还是开始的那个值return -1,else return ans;

官方题解:

 

 

 

 

 

 程序:

#include<bits/stdc++.h>
using namespace std;
int s1(string s)
{
    int ans=0x3f3f3f3f,n=s.size();
    if(n<3) return -1;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='O'&&i-1>=0&&i+1<n)
        {
            if(s[i-1]=='M'&&s[i+1]=='O') ans=min(ans,n-3);
            else if(s[i-1]=='O'&&s[i+1]=='M') ans=min(ans,n-1);
            else if(s[i-1]=='M'&&s[i+1]=='M') ans=min(ans,n-2);
            else if(s[i-1]=='O'&&s[i+1]=='O') ans=min(ans,n-2);
        }
    }
    if(ans>0x3f3f3f3f/2+1) return -1;
    else return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        cout<<s1(s)<<endl;
    }
    return 0;
}

 

标签:Operations,return,Contest,int,January,Moo,ans,Bronze
From: https://www.cnblogs.com/wjk53233/p/17096580.html

相关文章