BanazadehAria's blog

By BanazadehAria, history, 5 years ago, In English

Hi, I have used the same logic as editorial in problem 5C but my code gets the wrong answer. https://codeforces.com/contest/5/problem/C

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;
int dp[MAXN];

int main()
{
    string str;cin >> str;
    stack<int> s;int maxs=0,su0;
    for(int i=0;i<str.size();++i){
        if(str[i]=='('){
            s.push(i);dp[i]=-1;
        }else{
            if(s.empty()) dp[i]=-1;
            else{
                int top = s.top();int res = i-top+1;
                if(top!=0 && str[top-1]==')' && dp[top-1]!=-1) res += dp[top-1];
                dp[i]=res;maxs=max(maxs,dp[i]);
            }
        }
    }cout << maxs;
}
  • Vote: I like it
  • +9
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by BanazadehAria (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Is this forum for help? or this forum is for getting downvote?

I think its just 20 line code.And i have linked the problem also.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Maybe you must two numbers? Lenght of longest substring and number of such substrings