2023-07-14 Today's Three Questions - Catch Up

Link:

1419. Counting Frogs

Title meaning:

Given a string, each entry will be read into croak in sequence. After reading one, the next one can be read. How many entries are required to read this string, illegal input and output -1

untie:

There are five characters in total, and they are not repeated. They are directly stored in an array. Every time a croak is completed, if there is a case of temp in the array, you need temp+1 entry, and the maximum value is ans (like this time the Blue Bridge Cup National Competition is There is a question similar to this one, but the characters in it are repeated)

Actual code:

#include<iostream>
using namespace std;
int jsq[5];//croak
int minNumberOfFrogs(string croakOfFrogs)
{
    int lg=croakOfFrogs.length();
    int ans=0;
    for(int i=0;i<lg;i++)
    {
        switch(croakOfFrogs[i])
        {
            case 'c':
                jsq[0]++;
                break;
            case 'r':
                if(jsq[0])
                {
                    jsq[0]--;jsq[1]++;
                }
                else return -1;
                break;
            case 'o':
                if(jsq[1])
                {
                    jsq[1]--;jsq[2]++;
                }
                else return -1;
                break;
            case 'a':
                if(jsq[2])
                {
                    jsq[2]--;jsq[3]++;
                }
                else return -1;
                break;
            case 'k':
                if(jsq[3])
                {
                    int temp=0;
                    jsq[3]--;jsq[4]++;
                    for(int j=0;j<4;j++) temp+=jsq[j];
                    ans=max(ans,temp+1);
                }
                else return -1;
                break;
        }
    }
    for(int i=0;i<4;i++) if(jsq[i]) return -1;
    return ans;
}
int main()
{
    string s;cin>>s;
    
    int ans= minNumberOfFrogs(s);
    cout<<ans<<endl;
    return 0;
}

limit:

  • 1 <= croakOfFrogs.length <= 105
  • The only characters in the string are 'c', 'r', 'o', 'a'or'k'

Guess you like

Origin blog.csdn.net/Fei_WuYan/article/details/131733308