Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c++ - Tried counting depth of brackets using array and not stack

The function is to count depth of brackets and return the position of maximum and earliest depth.

enter code here

#include<iostream>
#include<string.h>
using namespace std;
int main() {
    int  n = 0, i =0, count = 0, maxi = 0, index = 0;
    string str;
    cout << "Enter n";
    cin >> n;
    cout << "Enter the brackets";
    cin >> str;
    while (n--) {

        for (i = 0; i < n; i++) {
            if (str[i] == 1) {
                count = 0;
                while (str[i] != 2) {
                    count++;
                    i++;
                }
                if (count > maxi) {
                    maxi = count;
                    index = i - count;
                }
            }

        }
    }
    cout << "depth= " << maxi << endl << "starting index=" << index;
    return 0;
}

//not sure what went wrong with logic.. I counted the open brackets and as soon closed bracket appears, count stops and the index no. and count is passed. PS 1 means open bracket and 2 means closed bracket.

question from:https://stackoverflow.com/questions/65857181/tried-counting-depth-of-brackets-using-array-and-not-stack

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have difficulty in understanding you logic, and therefore jump into some of my own intepretation.

  • I make n the number of strings to be input.
  • Each string use str.size() to get the length for testing.
  • Change you '1' to '<' and '2' to '>', thinking that you are finding the length between <..>.
  • move the output inside the while loop n, simply for easies to observe, you may bring this line back to the end of the program. It won'e change the execution.

The code I changed to:

 #include<iostream>
 #include<string>
 using namespace std;
 int main() {
    int  n = 0, i =0, count = 0, maxi = 0, index = 0;
    string str;
    char bra='<', key='>';  // change the delimiters
    cout << "Enter test runs : n = ";
    cin >> n;               // assume n the number of test runs
    while (n--)
        {
          cout << "Enter the brackets : ";
          cin >> str;

          for (i = 0; i < str.size(); i++) {  //use str.size();
          if (str[i] == bra) {

                count = 0;
                while (str[++i] != key) { count++; }
                if (count > maxi) {
                    maxi = count;
                    index = i - count;
                }
            }
        }
        cout << "depth= " << maxi << endl << "starting index = " << index <<std::endl;
    }
    return 0;
}

A test:

$ ./a.exe
Enter test runs : n = 2
Enter the brackets : First<1234>second<1234567>end1
depth= 7
starting index = 18
Enter the brackets : Thrid<123456789>forth<12345>end2
depth= 9
starting index = 6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...