Pagini recente » Cod sursa (job #193826) | Cod sursa (job #2015443) | Cod sursa (job #1808016) | Cod sursa (job #76972) | Cod sursa (job #2667009)
#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;
}