Cod sursa(job #338982)

Utilizator drag0s93Mandu Dragos drag0s93 Data 7 august 2009 17:55:12
Problema Lista lui Andrei Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include<cstdio>

using namespace std;

#define IN "nrcuv.in","r",stdin
#define OUT "nrcuv.out","w",stdout
int N , M ;
int hash[29][29];
long long V[2020][29];
bool cautare(int ch,int cautat)
{
	for(int i = 1 ; i <= hash[ch][0] ; ++i)
		if(hash[ch][i] == cautat)
			return false;
	return true;
}
int main()
{
	freopen(IN);
	freopen(OUT);
	scanf("%d%d\n",&N,&M);
	char ch1 , ch2;
	for(int i = 1 ; i <= M ; ++i)
	{
		scanf("%c %c\n",&ch1,&ch2);
		bool ok = true;
		for(int p = 1 ; p <= hash[ch1 - 'a' + 1][0]; ++p)
			if(hash[ch1 - 'a' + 1][p] == ch2 - 'a' + 1){
				ok = false;
				break;
			}
		hash[ch1 - 'a' + 1][0]++;
		if(ok == false)
			continue;
		if(ch1 != ch2){
			hash[ch2 - 'a' + 1][0]++;
			hash[ch2 - 'a' + 1][hash[ch2 - 'a' + 1][0]] = ch1 - 'a' + 1;
		}
		hash[ch1 - 'a' + 1][hash[ch1 - 'a' + 1][0]] = ch2 - 'a' + 1;
	}
	/*
	for(int i = 1 ; i <= 27 ; ++i)
	{
		printf("%d ",hash[i][0]);
		for(int j = 1 ;j <= 27 ; ++j)
			printf("%d ",hash[i][j]);
		printf("\n");
	}*/
	for(int i = 1; i <= 26 ; ++i)
		V[1][i] = 1;
	for(int i = 2 ; i <= N ; ++i)
		for(int j = 1 ; j <= 26 ; ++j){
			for(int p = 1 ; p <= 26 ; ++p)
				if(cautare(p,j) == true)
					V[i][j] += V[i - 1][p];
		}
	long long rez = 0;
	for(int i = 1 ;i <= 26 ; ++i)
		rez += (V[N][i]  % 104659);
	printf("%lld\n",rez);
	return 0;
}