Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- #define M 1000000007
- using namespace std;
- char s[400];
- int nrstele,n,P,cn;
- long long nrdrep=1, ct[1001];
- struct info
- {
- char cod;
- int H, V;
- };
- struct arbore
- {
- info date;
- arbore *st, *dr;
- }*rad, *p;
- int produs(int x, int y)
- {
- long long x_ll=x;
- long long y_ll=y;
- return (x_ll*y_ll)%M;
- }
- int cat(int &cn, int n)
- {
- if(n<=cn) return ct[n];
- else
- {
- for(int i=cn+1;i<=n;i++)
- {
- ct[i]=0;
- for(int j=0;j<=i-1;j++)
- ct[i]=(ct[i]+(ct[j]*ct[i-j-1])%M)%M;
- }
- cn=n;
- return ct[cn];
- }
- }
- int numar(char *s, int &i)
- {
- int nr=0;
- if(!('0'<=s[i] && s[i]<='9')) exit(0);
- while('0'<=s[i] && s[i]<='9')
- {
- nr=nr*10+s[i]-'0';
- i++;
- }
- return nr;
- }
- void creare_arbore(arbore*rad, int &i)
- {
- if(s[i]=='H')
- {
- rad->date.cod='H';
- i++;
- rad->date.H=numar(s,i);
- rad->date.V=1;
- rad->st= new arbore;
- creare_arbore(rad->st, i);
- rad->dr=new arbore;
- creare_arbore(rad->dr,i);
- }
- else
- if(s[i]=='V')
- {
- rad->date.cod='V';
- i++;
- rad->date.V=numar(s,i);
- rad->date.H=1;
- rad->st= new arbore;
- creare_arbore(rad->st, i);
- rad->dr=new arbore;
- creare_arbore(rad->dr,i);
- }
- else if(s[i]=='*')
- {
- rad->date.cod='*';
- i++;
- rad->date.V=1;
- rad->date.H=1;
- rad->st=NULL;
- rad->dr=NULL;
- nrstele++;
- }
- }
- void preordine_arbore(arbore*rad)
- {
- if(rad->date.cod=='H')
- {
- cout<<'H'<<rad->date.H;
- preordine_arbore(rad->st);
- preordine_arbore(rad->dr);
- }
- else if(rad->date.cod=='V')
- {
- cout<<'V'<<rad->date.V;
- preordine_arbore(rad->st);
- preordine_arbore(rad->dr);
- }
- else cout<<'*';
- }
- void calcul_arbore(arbore* rad, int &Hs, int &Vs, int &egale )
- {
- int H, V, e;
- if(rad->date.cod=='*')
- {
- Hs=1, Vs=1, egale=1;
- }
- else
- {
- egale=1;
- calcul_arbore(rad->st, H, V, e);
- Hs=max(rad->date.H, H);
- Vs=max(rad->date.V, V);
- if(rad->date.cod==rad->st->date.cod)
- egale+=e;
- else
- nrdrep=produs(nrdrep, cat(cn,e));
- calcul_arbore(rad->dr, H, V, e);
- if(rad->date.cod=='H')
- {
- Hs+=H;
- Vs=max(Vs, V);
- }
- else
- {
- Hs=max(Hs,H);
- Vs+=V;
- }
- if(rad->date.cod==rad->dr->date.cod)
- {
- egale+=e;
- }
- else nrdrep=produs(nrdrep, cat(cn,e));
- }
- }
- void rearanjare_arbore(arbore *par, int nod, arbore*&rad)
- {
- arbore*p,*q;
- while(rad->st!=NULL && rad->date.cod==rad->st->date.cod)
- {
- p=rad->st;
- q=rad->st->dr;
- p->dr=rad;
- rad->st=q;
- if(rad->date.cod=='H')
- {
- rad->date.H-=p->date.H;
- if(rad->date.H<=0) exit(0);
- }
- else
- {
- rad->date.V-=p->date.V;
- if(rad->date.V<=0) exit(0);
- }
- rad=p;
- if(par)
- {
- if(nod==1) par->st=rad;
- else par->dr=rad;
- }
- }
- if(rad->st)
- rearanjare_arbore(rad,1,rad->st);
- if(rad->dr)
- rearanjare_arbore(rad, 2, rad->dr);
- }
- int main()
- {
- int Hs, Vs, egale;
- rad=new arbore;
- cn=2;
- ct[0]=1, ct[1]=1, ct[2]=2;
- cin>>P;
- cin>>s;
- n=strlen(s);
- int i=0;
- creare_arbore(rad,i);
- if(P==1)
- {
- cout<<nrstele; return 0;
- }
- calcul_arbore(rad, Hs, Vs, egale);
- if(P==2)
- {
- cout<<Hs<<" "<<Vs;return 0;
- }
- if(P==3)
- {
- cout<<produs(nrdrep, cat(cn, egale));
- return 0;
- }
- if(P==4)
- {
- rearanjare_arbore(NULL, 3, rad);
- preordine_arbore(rad);
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement