Cod sursa(job #12695)

Utilizator raula_sanChis Raoul raula_san Data 4 februarie 2007 17:20:34
Problema Lista lui Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<stdio.h>
#include<string.h>

#define cnst 104659
#define dim 1<<10

int N, M;
long A[dim][1<<5], s;
char L[1<<5][1<<5];

int main()
{
    freopen("nrcuv.in", "r", stdin);
    freopen("nrcuv.out", "w", stdout);
    
    scanf("%d %d\n", &N, &M);
    
    char a, b;
    int i, j, k, x, y;
    
    for(i=1; i<=M; ++i)
    {
             scanf("%c %c\n", &a, &b);
             x = (int)a-'a'+1;
             y = (int)b-'a'+1;
             
             if( !strchr(L[x], b) ) L[x][strlen(L[x])] = b;
             if( !strchr(L[y], a) ) L[y][strlen(L[y])] = a;
    }
    
    for(i=1; i<=26; ++i) A[1][i] = 1; // nr de cuv care au l = 1 si ultima litera i este 1
        
    for(i=2; i<=N; ++i)
             for(j=1; j<=26; ++j)
             {
                      s = 0;
                      for(k=1; k<=26; ++k)
                      {
                               a = (char) k+'a'-1;
                               
                               if( !strchr(L[j], a) )
                               {
                                   s += A[i-1][k];
                                   s -= s >= cnst ? cnst : 0;
                               }
                      }
                      A[i][j] += s;
                      A[i][j] -= A[i][j] >= cnst ? cnst : 0;
             }
    s = 0;
	for(i=1; i<=26; ++i)
    {
             s += A[N][i];
             s -= s >= cnst ? cnst : 0;
    }
    
    printf("%ld", s);
    fclose(stdin); fclose(stdout);
    return 0;
}