Cod sursa(job #1071335)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 ianuarie 2014 21:38:12
Problema ADN Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.42 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <string.h>

using namespace std;

const char infile[] = "adn.in";
const char outfile[] = "adn.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 18;
const int MAXL = 30005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, Length[MAXN], Pi[MAXN][MAXL], erasedMask, C[MAXN][MAXN], dp[1 << MAXN][MAXN], Father[1 << MAXN][MAXN];
char s[MAXN][MAXL];

inline void BuildPrefix(const char *A, int *actPi, const int length) {
    int K = 0;
    for(int i = 2 ; i <= length ; ++ i) {
        while(K > 0 && A[K+1] != A[i])
            K = actPi[K];
        if(A[K+1] == A[i])
            ++ K;
        actPi[i] = K;
    }
}

inline int KMP(const int &firstNode, const int &secondNode) {
    int K = 0;
    for(int i = 1 ; i <= Length[secondNode] ; ++ i) {
        while(K > 0 && s[firstNode][K + 1] != s[secondNode][i])
            K = Pi[firstNode][K];
        if(s[firstNode][K + 1] == s[secondNode][i])
            ++ K;
        if(K == Length[firstNode]) {
            /// full match => we must get rid of the firstNode(string)
            erasedMask |= (1 << firstNode);
            return K;
        }
    }
    return K;
    /// in case of non-match, the value of K represents the
    /// longest prefix of secondNode which is actually a suffix of firstNode
}

inline void buildSolution(const int &conf, const int &Node) {
    if(Father[conf][Node] == -1) { /// we've just found the root
        fout << s[Node] + 1;
        return;
    }
    int father = Father[conf][Node];
    buildSolution(conf ^ (1 << Node), father);
    /// from father to Node
    for(int i = C[father][Node] + 1 ; i <= Length[Node] ; ++ i)
        fout << s[Node][i];
}

int main() {
    fin >> N;
    for(int i = 0 ; i < N ; ++ i) {
        fin >> (s[i] + 1);
        Length[i] = strlen(s[i] + 1);
        BuildPrefix(s[i], Pi[i], Length[i]);
    }
    for(int i = 0 ; i < N ; ++ i)
        for(int j = 0 ; j < N ; ++ j)
            C[i][j] = (i == j) ? -oo : KMP(j, i);
    memset(dp, oo, sizeof(dp));
    memset(Father, -1, sizeof(Father));
    for(int i = 0 ; i < N ; ++ i)
        dp[1 << i][i] = Length[i];
    int maxConf = (1 << N);
    int t;
    /// Hamiltonian Path Stuff
    for(int conf = 0 ; conf < maxConf ; ++ conf) {
        for(int i = 0 ; i < N ; ++ i)
            if(conf & (1 << i))
                for(int k = 0 ; k < N ; ++ k)
                    if(conf & (1 << k)) {
                        if((t = dp[conf ^ (1 << i)][k] - C[k][i] + Length[i]) < dp[conf][i]) {
                            dp[conf][i] = t;
                            Father[conf][i] = k;
                        }
                    }
    }
    int cycleEnd = 0, finalConf = (maxConf - 1) ^ erasedMask;
    /// now choose a Node to start
    for(int i = 0; i < N ; ++ i)
        if(dp[finalConf][cycleEnd] > dp[finalConf][i])
            cycleEnd = i;
    buildSolution(finalConf, cycleEnd);
    fin.close();
    fout.close();
    return 0;
}