[2023 King's Road Data Structure] [Stack, Queue and Array 03] Complete implementation of C and C++ (can be run directly)

~~~How can the writing style end in a dull way, and the story does not recognize ordinary at the beginning ✌✌✌

If you need the complete code, you can follow the public account below, and reply "code" in the background to get it. Aguang is looking forward to your visit~

topic:
insert image description here

Problem solving ideas:

>判断子缀和是否小于0
>如果成立则为不合法序列

Code:

#include <iostream>
using namespace std;

void Judge(string s)
{
    int sum = 0;
    for (int i = 0; i < s.length(); i++)
    {

        if (s[i] == 'I')
        {
            sum += 1;
        }
        else
        {
            sum -= 1;
        }
        if (sum < 0)
        {
            cout << "不合法序列" << endl;
            return;
        }
    }

    if (sum == 0)
    {
        cout << "合法序列" << endl;
        return;
    }

    cout << "不合法序列" << endl;
}

int main()
{
    string s1 = "IOIIOIOO";
    string s2 = "IOOIOIIO";
    string s3 = "IIIOIOIO";
    string s4 = "IIIOOIOO";
    Judge(s1);
    Judge(s2);
    Judge(s3);
    Judge(s4);
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124439025