ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1183. Brackets Sequence

Time limit exceeded on test 9
Posted by Andrei Rezus 2 Dec 2020 20:14
Hi! This is my code for 1183,can someone help me to solve it? I have time limit exceeded on test 9,please help!
#include <stdio.h>
#include <string.h>
//#include <limits.h>
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
int L,memo[100][100];
char S[101];
/*int min(int x,int y)
{
    if(x<y)
        return x;
    else
        return y;
}*/
int rezolva(int s, int e)
{
    if(s>e)
        return 0;
    int ret = memo[s][e];
    if(ret==-1){
        ret = 1+rezolva(s+1,e);
        if(S[s]=='(' || S[s]=='[')
            {
            for(int i = s+1;i<=e;i++)
                if((S[s]=='(' && S[i]==')') || (S[s]=='[' && S[i]==']'))
                    ret = MIN(ret,rezolva(s+1,i-1)+rezolva(i+1,e));
        }
    }
    return ret;
}
void print(int s, int e)
{
    if(s>e) return;
    int best = rezolva(s,e);
    if(1+rezolva(s+1,e)==best)
    {
        if(S[s]=='(' || S[s]==')')
            {
            putchar('(');
            putchar(')');
        }
        else
            {
            putchar('[');
            putchar(']');
        }
        print(s+1,e);
        return 0;
    }
    for(int i = s+1;i<=e;++i)
        {
        if(((S[s]=='(' && S[i]==')') || (S[s]=='[' && S[i]==']')) && best==rezolva(s+1,i-1)+rezolva(i+1,e)){
            if(S[s]=='(')
                {
                putchar('(');
                print(s+1,i-1);
                putchar(')');
                print(i+1,e);
            }
            else
            {
                putchar('[');
                print(s+1,i-1);
                putchar(']');
                print(i+1,e);
            }
            return 0;
        }
    }
}
int main()
{
    scanf("%s",S);
    L = strlen(S);
    memset(memo,-1,sizeof(memo));
    print(0,L-1);
    putchar('\n');
    return 0;
}