Guest User

Parole_saturnine

a guest
Sep 8th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <cstdio>
  2. const int MAXM=1005;
  3.  
  4. int N, M, S[MAXM], mem[MAXM][MAXM];
  5.  
  6. int solve(int,int);
  7.  
  8. int main()
  9. {
  10.     freopen("input.txt","r",stdin);
  11.     freopen("output.txt","w",stdout);
  12.    
  13.     for(int i=0; i<MAXM; i++)
  14.             for(int j=0; j<MAXM; j++)
  15.                     mem[i][j]=-1;
  16.    
  17.     scanf("%d %d", &M, &N);
  18.    
  19.     for(int i=1; i<=M; i++) scanf("%d", S+i);
  20.    
  21.     printf("%d\n", solve(0,0));
  22.    
  23.     return 0;
  24. }
  25.  
  26. int solve(int n, int s)
  27. {
  28.     if(mem[n][s]!=-1) return mem[n][s];
  29.     if(s==M) return mem[n][s]=0;
  30.     if(n==N) return mem[n][s]=1;
  31.    
  32.     //Estendo il matching
  33.     int a=solve(n+1,s+1);
  34.    
  35.     //Annullo il matching corrente calcolando quello nuovo
  36.     int nuovaLettera=(S[s+1]==1)? 0:1, i, nuovaS;
  37.     bool found=false;
  38.    
  39.     for(nuovaS=s; nuovaS>0 && !found; nuovaS--){
  40.         if(S[nuovaS]==nuovaLettera)
  41.             for(i=1, found=true; i<nuovaS && found; i++)
  42.                  if(S[s+1-i]!=S[nuovaS-i]) found=false;
  43.     }
  44.    
  45.     int b=solve(n+1,nuovaS);
  46.    
  47.     return mem[n][s]=(a+b)%2011;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment