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

why WA9???
Posted by Elmi Ahmadov 6 Mar 2012 17:22
I check some tests where I get from forum, but my program got WA9.

it is my code :

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>

#define For(i , n) for (int i = 0; i < n; i++)
#define FOR(i , n) for (int i = n - 1; i >= 0; i--)

using namespace std;

struct Char
{
  char ch;
  int used;
  Char(char c,int u) { ch = c; used = u; }
};

string seq;
vector<Char> stck;

void insert(int i)
{
   if (stck[i].ch == '(') stck.insert(stck.begin() + i + 1, Char(')', 1));
   else if (stck[i].ch == ')')
    {
     stck.erase(stck.begin() + i);
     stck.insert(stck.begin() + i , Char('(' , 1));
     stck.insert(stck.begin() + i + 1 , Char(')' , 1));
    }
   else if (stck[i].ch == '[') stck.insert(stck.begin() + i + 1, Char(']', 1));
   else if (stck[i].ch == ']')
    {
     stck.erase(stck.begin() + i);
     stck.insert(stck.begin() + i , Char('[' , 1));
     stck.insert(stck.begin() + i + 1 , Char(']' , 1));
    }
}

void search(char ch,int from)
{
  int res = 0 , pos = from;
  bool ok = true;
  FOR(i , from)
   if (stck[i].ch == ch && !stck[i].used) {
      stck[i].used = 1;
      pos = i,
      ok = false;
      break;
    }
  for (int i = from - 1; i > pos; i--)
   if (!stck[i].used)
    {
     stck[i].used = 1; ok = false;
     insert(i);
    }
   if (ok) stck[from - 1].used = 1 , insert(from - 1);
}

int main()
{
 cin>>seq;
 stck.clear();
 For(i , seq.length())
  {
    stck.push_back(Char(seq[i],0));
    if (stck.back().ch == ')') stck.back().used = 1 , search('(' , stck.size());
    else if (stck.back().ch == ']') stck.back().used = 1 , search('[' , stck.size());
  }
 For(i , stck.size())
  {
   if (!stck[i].used)
    {
     stck[i].used = 1;
     insert(i);
    }
  }
 For(i , stck.size())
  cout<<stck[i].ch;
 cout<<endl;

 return 0;
}

sorry for my english.