[question 10] bracket matching | string processing (Beijing Institute of Technology / Beijing University of Technology / programming method and practice / primary school)

Posted by harrylt on Fri, 17 Dec 2021 00:21:37 +0100

 

catalogue

preface

program

preface

There are ready-made solutions to this question on csdn, @ has Beibei AC today? The blogger wrote very well and posted a website

https://blog.csdn.net/weixin_43787043/article/details/108518190

Of course, I undoubtedly referred to her code, and then I drew and typed it myself, said my feelings and pasted the code.  

As like as two peas, the principle of parenthesis matching is that brackets matching is essentially a kind of dissipation, and the ones that will be eliminated will be eliminated. The characters that are encountered will disappear perfectly. The matching features will match the characteristics of the stack, so the brackets matching later will be, ()] {}, the principle is exactly the same, that is stack.

Stack learning, emm, this thing is too simple, not to mention the c + + encapsulated stack, just c language, get an array, and then cooperate with a pointer to be used as a stack.

In other words, in fact, we have been using various data structures for a long time, but we don't know it. From array to subsequence automata, they are all data structures. The so-called data structure is just a data combination method that people occasionally think of in the process of practice, and then find it very useful. Ah, this is recorded. An abstraction becomes a routine.

program

Because I knocked it myself, I changed the program.

In the past, I used to use functions because I didn't have enough ability to grasp the whole code of about 50-100 lines. Now, after 10 courses of programming and snake eating, I'm used to the whole code of 50-100 lines. If I work hard to control 1000 lines of modular code with function encapsulation, there's no problem. Sure enough, knock more to promote specific ideas, Repeated thinking will deepen the firmness of memory blocks. In computer science, hands-on is the key.

My change this time is mainly

  • Make modular things into a whole paragraph. If I can make the whole paragraph, I'm too lazy to spend on modules. Just make comments and modularization.
  • Added a lot of notes, very easy to understand.
  • Modify the structure of the comparison function because it is a short function and is written directly at the beginning.
  • I modified the function structure according to my own habits to make the structure clearer. In fact, mine looks a little more.
  • The problem is that I accidentally put a large array in main and burst the stack for the first time in my life

On the code

#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
#define SIZE 100002
int match(char ch1, char ch2) 
{ return (ch1 == '(' && ch2 == ')') ? 1 : 0; };
//The arrays that occupy the memory are put outside, otherwise the main stack will explode every minute. That's the long long long for me
char str[SIZE];
char stack[SIZE];
long long left[SIZE] = { 0 }, right[SIZE] = { 0 };

int main(void)
{
	//left/right is an array of simplified strings,
	//Coordinates: the remaining bracket values: the number of such reduced strings
	long long ct_empty = 0; //ct_empty record reduction
	int n;
	int i, j;
	int len_str;
	int top;
	int ct_remain; //Number of remaining parentheses
	char ch_top;
	bool ispure;
	long long ans;
	scanf("%d\n", &n);//Absorb characters to prevent confusion

	//Process each string and count the number of various strings
	for (i = 0; i < n; i++)
	{
		//Simplification
		memset(stack, 0, sizeof stack);//empty
		top = -1;
		ispure = true;
		scanf("%s", str);//Read and initialize. I originally wanted to use gets_s came, but Lexue couldn't recognize it
		len_str = strlen(str);
		for (j = 0; j < len_str; j++)//The characters in str are processed one by one to simplify str
		{
			if (top != -1 && match(stack[top], str[j]))//First judge whether it is empty, and then conduct matching comparison
				top--;//Not only don't read in, but also get one out
			else  //If it's empty or doesn't match, put this bracket in
				stack[++top] = str[j]; //Add before assign. Top always points to the top element
		}
		//The simplified strings are statistically classified
		if (top == -1)//Finally, if it is empty, count the empty string
			ct_empty++;
		else //Finally, the rest is left, and then judge whether it is all or the same
		{
			ch_top = stack[top];
			ct_remain = top + 1;
			while (top >= 0)//Traverse the elements in the stack
			{
				if (stack[top] != ch_top)//Not the same, change to the next one
				{
					ispure = false;
					break;
				}
				top--;  //For the time being, continue to traverse
			}
			if (ispure) //If you can walk down, ispure is true
			{
				if (ch_top == '(')
					left[ct_remain]++;
				else
					right[ct_remain]++;
			}	
		}
	}
	//Calculate the results with the statistically classified data
	ans = ct_empty / 2;
	for (i = 0; i < SIZE; i++)//If it is not filled, the default value is 0, which will not affect the data. Rest assured to traverse
		ans += left[i] > right[i] ? right[i] : left[i];

	printf("%lld\n", ans);
	return 0;
}

Topics: C