Cod sursa(job #1644501)

Utilizator KanghuAndre Popescu Kanghu Data 9 martie 2016 23:43:54
Problema Lista lui Andrei Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <iostream>
#include <fstream>

using namespace std;

int N, M;
int S;

char lit[2000][2];
int din[1000][26];

ifstream i("nrcuv.in");
ofstream o("nrcuv.out");

int backtrack(int x, char last)
{
    if(x == N)
    {
        S++;
        return 0;
    }

    if(din[x][last - 'a'])
    {
        return din[x][last - 'a'];
    }

    else
    {
        for(char a = 'a'; a <= 'z'; a++)
        {
            bool ok = true;

            if(x != 0)
            {
                for(int b = 0; b < M; b++)
                {
                    if(last == lit[b][0])
                    {
                        if(a == lit[b][1])
                        {
                            ok = false;
                        }
                    }

                    if(last == lit[b][1])
                    {
                        if(a == lit[b][0])
                        {
                            ok = false;
                        }
                    }
                }
            }

            if(ok)
            {
                din[x][a]++;
                backtrack(x + 1, a) + 1;
            }
        }
    }
}

int main()
{
    i >> N >> M;

    for(int a = 0; a < M; a++)
    {
        i >> lit[a][0] >> lit[a][1];
    }

    backtrack(0, 'a');

    int suma = 0;

    for(int a = 0; a < 26; a++)
    {
        suma = suma + din[1][a + 'a'];
    }

    o << S;

    return 0;
}