题目1342:寻找最长合法括号序列II---注意:不要求就近匹配,只要求( 在 )前面的任一个位置即可

#include<stdio.h>   
 
int main()
{      
    char str[1000000];
    while(scanf("%s",str)!=EOF)
    {
         
        int temp=0,ans=0;
          for(int i = 0; str[i]; i++) {
            if(str[i] == '(') {
                temp++;
            }
            else if(temp > 0) {
                temp--;//保证)前面有匹配的(的存在
                ans += 2;
            }
        }
     
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2017-03-14 16:46  贱人郭  阅读(104)  评论(0编辑  收藏  举报