sudt 括号东东(模拟题)

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1451

今天下午的比赛题,该说自己理解能力太弱还是题目描述有歧义,比赛一开始我就找到了这道题,读完题感觉很容易,于是就用栈模拟了一遍,结果WA,又重新理解了一遍题,感觉应该没什么问题,将各种可能的想了一遍,改完提交还是WA,然后再改,最后终于想不出来还有什么情况了,于是重新读题,还是没弄懂要求输出的是最长合法子串的个数,我一直输出的是合法子串的总数,其实中间我曾想过有可能是让输出最长合法子串的个数,但是不确定,而且题目中给出的是“以及合法子串的个数”,也没看到有人问,所以就一直在纠结这题,知道最后还是没能做出来。

赛后查了一下解题报告,才知道是让输出最长合法子串的个数,无语!!!

代码:

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define  N 1000004
using namespace std ;

char str[N] ;
stack<int>q ;
int num[N] ;
map<int,int>mp ;

int main()
{
    int i , len , t , MAX , ans ;

    while ( scanf ( "%s" , str ) != EOF )
    {
        len = strlen( str ) ;
        while( !q.empty()) q.pop();
        memset( num , 0 , sizeof( num )) ;
        for ( i = 0 ; i < len ; i++ )
        {
            if ( str[i] == '(' )
            {
                q.push( i ) ;
            }
            else
            {
                if ( !q.empty())
                {
                    t = q.top();
                    num[t] = num[i] = 1 ;
                    q.pop();
                }
            }
        }
        t = 0 ;
        mp.clear();
        for ( i = 0 ; i < len ; i++ )
        {
            //cout<<num[i]<<" ";
            if ( num[i] == 0 )
            {
                mp[t]++ ;
                t = 0 ;
            }
            else
            t++ ;
        }
        //cout<<endl ;
        if ( t != 0 )
        mp[t]++ ;

        map<int,int>::iterator it ;
        MAX = 0 ;
        for ( it = mp.begin() ; it != mp.end() ; it++ )
        {
            if( it->first > MAX )
            {
                MAX = it->first ;
                ans = it->second ;
            }
        }
        if ( MAX == 0 )
        {
            printf ( "%d %d\n" , 0 , 1 ) ;
        }
        else
        {
            printf ( "%d %d\n" , MAX , ans ) ;
        }
    }
    return 0 ;
}
原文地址:https://www.cnblogs.com/misty1/p/2878083.html