codeforces contest/5/problem/C

题意:求最长连续符合的(),和个数

思路:dp[i]表示以i为结尾的最长连续合法(),那么当我们匹配到一个()时,是不是dp[)的位置]=()的长度+dp[(前一个位置]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 stack<int  >a;
 4 int dp[1000006];
 5 int main()
 6 {
 7     string s;
 8     int i,j;
 9     int l;
10     cin>>s;
11     l=s.size();
12     int sum=0;
13     for(i=0;i<l;i++)
14     {
15         if(s[i]=='(')
16             a.push(i);
17         else
18         {
19             if(!a.empty())
20             {
21                   dp[i]=i-a.top()+1;
22                if(a.top()>0)
23                 dp[i]+=dp[a.top()-1];
24                a.pop();
25             }
26 
27         }
28     }
29     int Max=-1,ss=1;
30     for(i=0;i<l;i++)
31     {
32 
33         if(dp[i]>Max)
34         {
35             Max=dp[i];
36             ss=1;
37         }
38         else if(dp[i]==Max&&Max!=0)
39             ss++;
40     }
41     cout<<Max<<" "<<ss<<endl;
42     return 0;
43 }

 

posted on 2017-07-28 15:07  hhhhx  阅读(116)  评论(0编辑  收藏  举报

导航