Cod sursa(job #1259484)

Utilizator RenataRenata Renata Data 10 noiembrie 2014 02:13:33
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include<iostream>
#include<fstream>
using namespace std;
struct nod{char info;
           nod *urm;}*v;
void push (char a,nod *&stiva)
{   nod *temp;
   if(stiva==NULL)
   {
       stiva=new nod;
       stiva->info=a;
       stiva->urm=NULL;
   }
    else
    {temp=new nod;
    temp->info=a;
    temp->urm=stiva;
    stiva=temp;}
}
char pop (nod *&stiva)
{   char x;
    nod *temp;
    if(stiva==NULL)
        return -1;
    else
    {temp=stiva;
    stiva=stiva->urm;
    x=temp->info;
    delete temp;
    return x;
  }

}
int main()
{  char s[5000];
   int i,n,nr=0,nr2=0;
   ifstream f("par.in");
   f>>n;
   f>>s;
   f.close();
   for(i=0;i<n;i++)
      if(s[i]=='(')
        push(s[i],v);
      else
        if(pop(v)==-1)
           {push(s[i],v);
             nr++;}
    ofstream g("par.out");
    if(v==NULL)
        g<<0;
    else
      {if(nr%2==0) nr=nr/2;
       while(v)
       {nr2++;
       v=v->urm;}}
    nr2=nr2/2;
    g<<nr+nr2;
    g.close();
   return 0;
}