zoukankan      html  css  js  c++  java
  • 九度 1337 寻找最长合法括号序列

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

    在混乱中写完了最后一个循环,要时刻警惕越界。。   还有就是indent还是不会用,直接把代码缩进搞乱了

     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 ("%d", &N) != 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 = -1;
    42 int num = 0;
    43 i = j = 0;
    44 while (j <= len)
    45 {
    46 while (j<=len&&!flags[j])
    47 j++;
    48 i = j;
    49 while (j<=len&&flags[j])
    50 j++;
    51 if (j - i > max_len)
    52 {
    53 max_len = j - i ;
    54 num = 1;
    55 }
    56 else if (j - i == max_len)
    57 {
    58 num++;
    59 }
    60 i = j + 1;
    61 }
    62 printf("%d %d\n",max_len,num);
    63 }
    64 }



  • 相关阅读:
    场景调研
    手机搜狗输入法体验
    1到一个整数之间1的个数
    寻找水王
    成套卖书最大优惠问题
    面向对象--多态
    抽象类和接口
    面向对象思想--继承
    面向对象思想--封装
    变量和参数传递
  • 原文地址:https://www.cnblogs.com/yangce/p/2281687.html
Copyright © 2011-2022 走看看