Cod sursa(job #2667009)

Utilizator Tudor06MusatTudor Tudor06 Data 2 noiembrie 2020 19:06:51
Problema Lista lui Andrei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const int NMAX = 1000;
const int SIGMA = 26;
const int MOD = 104659;

int dp[NMAX + 1][SIGMA];

vector <int> perechi[SIGMA];

int main() {
    ifstream fin( "nrcuv.in" );
    ofstream fout( "nrcuv.out" );
    int n, m, i;
    char a, b;
    fin >> n >> m;
    for ( i = 0; i < m; i ++ ) {
        fin >> a >> b;
        perechi[a - 'a'].push_back( b - 'a' );
        perechi[b - 'a'].push_back( a - 'a' );
    }

    for ( i = 0; i < SIGMA; i ++ ) {
        sort( perechi[i].begin(), perechi[i].end() );
    }
    ///dp[i][j] = nr de cuvinte de lungime n si ultima litera este j
    for ( i = 0; i < SIGMA; i ++ )
        dp[1][i] = 1;
    for ( i = 2; i <= n; i ++ ) {
        for ( int j = 0; j < SIGMA; j ++ ) {
            int x = 0;
            int y = 0;
            while ( x < SIGMA ) {
                if ( y >= perechi[j].size() || x != perechi[j][y] )
                    dp[i][j] = ( dp[i][j] + dp[i - 1][x] ) % MOD;
                else {
                    while ( y < perechi[j].size() && x == perechi[j][y] )
                        y ++;
                }
                x ++;
            }
        }
    }
    int s = 0;
    for ( i = 0; i < SIGMA; i ++ ) {
        s = ( s + dp[n][i] ) % MOD;
    }
    fout << s;
    return 0;
}