九度 1342 寻找最长合法括号序列II

http://ac.jobdu.com/problem.php?id=1342

开始时没太读懂题, “去掉一些括号" 其实换句话说,就是看看有多少括号匹配过。。。 比上一个容易些,不用管连续不连续,用上一题的代码就可以

 1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stack>
5 using namespace std;
6 int N;
7 char str[1000002];
8 bool flags[1000002] = { false };
9
10 stack < int >S;
11 bool match(char a,char b)
12 {
13 if(a=='('&&b==')')
14 return true;
15 return false;
16 }
17 int main ()
18 {
19 while (scanf("%s",str)!= EOF)
20 {
21 //scanf ("%s", str);
22 while (!S.empty ())
23 S.pop ();
24 memset (flags, false, sizeof (flags));
25 S.push (0);
26 int i, j;
27 int len = strlen (str) - 1;
28 for (i = 1; i <= len; i++)
29 {
30 if (S.empty ())
31 S.push (i);
32 else if (match (str[S.top ()], str[i]))
33 {
34 flags[i] = true;
35 flags[S.top ()] = true;
36 S.pop ();
37 }
38 else
39 S.push (i);
40 }
41 int max_len=0;
42 for(i=0;i<=len;i++)
43 if(flags[i])
44 max_len++;
45 printf("%d\n",max_len);
46 }
47
48 }
原文地址:https://www.cnblogs.com/yangce/p/2281712.html