problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
acwing::acwing4207类 参考

AcWing 4207. 最长合法括号子序列 更多...

#include <acwing.h>

静态 Public 成员函数

static int main (istream &, ostream &)
 

详细描述

AcWing 4207. 最长合法括号子序列

在文件 acwing.h264 行定义.

成员函数说明

◆ main()

int acwing::acwing4207::main ( istream &  cin,
ostream &  cout 
)
static

在文件 acwing.cpp652 行定义.

652 {
653 string str;
654 cin >> str;
655 auto *in_sub = new bool[str.length()];
656 int count = 0;
657 memset(in_sub, 0, str.length() * sizeof(bool));
658
659 int prev_left = -1;
660 for(int i = 0; i < str.length(); i++) {
661 if(str[i] == '(') {
662 prev_left = i;
663 break;
664 }
665 }
666 for(int i = prev_left; i < str.length(); i++) {
667 if(str[i] == ')' && prev_left != -1) {
668 count += 2;
669 in_sub[prev_left] = true;
670 in_sub[i] = true;
671 for(int j = prev_left + 1; j < i; j++) {
672 if(str[j] == '(') {
673 prev_left = j;
674 break;
675 }
676 }
677 if(in_sub[prev_left]) {
678 prev_left = -1;
679 }
680 } else if(prev_left == -1 && str[i] == '(') {
681 prev_left = i;
682 }
683 }
684 cout << count;
685 delete[] in_sub;
686 return 0;
687 }

被这些函数引用 acwing::TEST().


该类的文档由以下文件生成: