Advertisement
nicuvlad76

Untitled

Mar 12th, 2023
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. #define M 1000000007
  4. using namespace std;
  5. char s[400];
  6. int nrstele,n,P,cn;
  7. long long nrdrep=1, ct[1001];
  8. struct info
  9. {
  10.     char cod;
  11.     int H, V;
  12. };
  13. struct arbore
  14. {
  15.     info date;
  16.     arbore *st, *dr;
  17. }*rad, *p;
  18.  
  19. int produs(int x, int y)
  20. {
  21.     long long x_ll=x;
  22.     long long y_ll=y;
  23.     return (x_ll*y_ll)%M;
  24. }
  25. int cat(int &cn, int n)
  26. {
  27.     if(n<=cn) return ct[n];
  28.     else
  29.     {
  30.         for(int i=cn+1;i<=n;i++)
  31.         {
  32.             ct[i]=0;
  33.             for(int j=0;j<=i-1;j++)
  34.                 ct[i]=(ct[i]+(ct[j]*ct[i-j-1])%M)%M;
  35.         }
  36.         cn=n;
  37.         return ct[cn];
  38.     }
  39. }
  40. int numar(char *s, int &i)
  41. {
  42.     int nr=0;
  43.     if(!('0'<=s[i] && s[i]<='9')) exit(0);
  44.     while('0'<=s[i] && s[i]<='9')
  45.     {
  46.         nr=nr*10+s[i]-'0';
  47.         i++;
  48.     }
  49.     return nr;
  50. }
  51.  
  52. void creare_arbore(arbore*rad, int &i)
  53. {
  54.     if(s[i]=='H')
  55.     {
  56.         rad->date.cod='H';
  57.         i++;
  58.         rad->date.H=numar(s,i);
  59.         rad->date.V=1;
  60.         rad->st= new arbore;
  61.         creare_arbore(rad->st, i);
  62.         rad->dr=new arbore;
  63.         creare_arbore(rad->dr,i);
  64.     }
  65.     else
  66.         if(s[i]=='V')
  67.     {
  68.         rad->date.cod='V';
  69.         i++;
  70.         rad->date.V=numar(s,i);
  71.         rad->date.H=1;
  72.         rad->st= new arbore;
  73.         creare_arbore(rad->st, i);
  74.         rad->dr=new arbore;
  75.         creare_arbore(rad->dr,i);
  76.     }
  77.     else if(s[i]=='*')
  78.     {
  79.         rad->date.cod='*';
  80.         i++;
  81.         rad->date.V=1;
  82.         rad->date.H=1;
  83.         rad->st=NULL;
  84.         rad->dr=NULL;
  85.         nrstele++;
  86.     }
  87. }
  88. void preordine_arbore(arbore*rad)
  89. {
  90.     if(rad->date.cod=='H')
  91.     {
  92.         cout<<'H'<<rad->date.H;
  93.         preordine_arbore(rad->st);
  94.         preordine_arbore(rad->dr);
  95.     }
  96.     else if(rad->date.cod=='V')
  97.     {
  98.         cout<<'V'<<rad->date.V;
  99.         preordine_arbore(rad->st);
  100.         preordine_arbore(rad->dr);
  101.     }
  102.     else cout<<'*';
  103. }
  104. void calcul_arbore(arbore* rad, int &Hs, int &Vs, int &egale )
  105. {
  106.     int H, V, e;
  107.     if(rad->date.cod=='*')
  108.     {
  109.         Hs=1, Vs=1, egale=1;
  110.     }
  111.     else
  112.     {
  113.       egale=1;
  114.       calcul_arbore(rad->st, H, V, e);
  115.       Hs=max(rad->date.H, H);
  116.       Vs=max(rad->date.V, V);
  117.       if(rad->date.cod==rad->st->date.cod)
  118.                  egale+=e;
  119.       else
  120.          nrdrep=produs(nrdrep, cat(cn,e));
  121.  
  122.       calcul_arbore(rad->dr, H, V, e);
  123.       if(rad->date.cod=='H')
  124.       {
  125.           Hs+=H;
  126.           Vs=max(Vs, V);
  127.       }
  128.       else
  129.       {
  130.           Hs=max(Hs,H);
  131.           Vs+=V;
  132.       }
  133.       if(rad->date.cod==rad->dr->date.cod)
  134.       {
  135.           egale+=e;
  136.       }
  137.       else nrdrep=produs(nrdrep, cat(cn,e));
  138.     }
  139. }
  140. void rearanjare_arbore(arbore *par, int nod, arbore*&rad)
  141. {
  142.     arbore*p,*q;
  143.     while(rad->st!=NULL && rad->date.cod==rad->st->date.cod)
  144.     {
  145.         p=rad->st;
  146.         q=rad->st->dr;
  147.         p->dr=rad;
  148.         rad->st=q;
  149.         if(rad->date.cod=='H')
  150.         {
  151.             rad->date.H-=p->date.H;
  152.             if(rad->date.H<=0) exit(0);
  153.         }
  154.         else
  155.         {
  156.             rad->date.V-=p->date.V;
  157.             if(rad->date.V<=0) exit(0);
  158.         }
  159.         rad=p;
  160.         if(par)
  161.         {
  162.             if(nod==1) par->st=rad;
  163.             else par->dr=rad;
  164.         }
  165.     }
  166.     if(rad->st)
  167.         rearanjare_arbore(rad,1,rad->st);
  168.     if(rad->dr)
  169.         rearanjare_arbore(rad, 2, rad->dr);
  170. }
  171. int main()
  172. {
  173.    int Hs, Vs, egale;
  174.    rad=new arbore;
  175.    cn=2;
  176.    ct[0]=1, ct[1]=1, ct[2]=2;
  177.    cin>>P;
  178.    cin>>s;
  179.    n=strlen(s);
  180.    int i=0;
  181.    creare_arbore(rad,i);
  182.    if(P==1)
  183.    {
  184.        cout<<nrstele; return 0;
  185.    }
  186.    calcul_arbore(rad, Hs, Vs, egale);
  187.    if(P==2)
  188.    {
  189.        cout<<Hs<<" "<<Vs;return 0;
  190.    }
  191.    if(P==3)
  192.    {
  193.        cout<<produs(nrdrep, cat(cn, egale));
  194.        return 0;
  195.    }
  196.    if(P==4)
  197.    {
  198.        rearanjare_arbore(NULL, 3, rad);
  199.        preordine_arbore(rad);
  200.        return 0;
  201.    }
  202.   return 0;
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement