Language: C

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

int dp[2][18][65600];
bool bit[65600][18];
char s[130][18];
int m, n, maxbt, len[130];
int top[65600][18], btm[65600][18];

void intit()
{
    int i, j, p;
    for( i = 0; i <= maxbt; i ++ ){
        int tmp = i;
        for( j = 0; j < m; j ++ ){
            bit[i][j] = tmp%2;
            tmp /= 2;
        }
        for( j = 1, top[i][0] = 0; j <= m; j ++ ){
            if( bit[i][j-1] == 1 ){
                top[i][j] = top[i][j-1]+1;
            }
            else top[i][j] = top[i][j-1];
        }
        for( j = m-1, btm[i][0] = 0, p = 1; j >= 0; j --, p ++ ){
            if( bit[i][j] == 1 ){
                btm[i][p] = btm[i][p-1]+1;
            }
            else btm[i][p] = btm[i][p-1];
        }
    }
}

int caltop( int y, int st )
{
    return top[st][y];
}

int calbtm( int y, int st )
{
    return btm[st][y];
}

int lf( int x, int y, int st )
{
    if( y == 0 ) return 0;
    if( bit[st][0] == 0 )return 0;
    int tmp = caltop(y, st);
    if( tmp == 0 )return 0;
    if( s[x][tmp] == s[x][tmp-1] )return 1;
    else return 0;
}

int up( int x, int y, int st )
{
    int tmp = calbtm( m-y, st );
    int tmp1 = caltop( y, st );
    if( x == 0 ) return 0;
    if( bit[st][m-1] == 0 ) return 0;
    if( s[x][tmp1] == s[x-1][len[x-1]-tmp] )return 1;
    else return 0;
}


int main()
{
    int i, j, p, tmp, k;
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(~scanf("%d%d", &n, &m)){
        maxbt = (1<<m) - 1;
        intit();
        for( i = 1; i <= n; i ++ ){
            scanf("%s", s[i]);
            len[i] = strlen(s[i]);
        }
        for( i = 0; i <= 17; i ++ )
            for( j = 0; j <= 65536; j ++ )
            dp[0][i][j] = dp[1][i][j] = -999999999;
        dp[1][0][0] = 0; dp[1][0][1] = 0;//第一行第一个放或者不放都是0
        for( i = 1; i <= n; i ++ ){
            p = i%2;
            for( j = 0; j < m; j ++ ){
                for( k = 0; k <= maxbt; k ++ ){
                    if( i == 1 && j == 0 )continue;
                    tmp = (k<<1)&maxbt;
                    if( i == 1 ){
                        if( caltop( j, k ) > len[i] ) continue;
                        dp[p][j][tmp] = max(dp[p][j][tmp], dp[p][j-1][k]);// not draw
                        if( caltop( j, k ) != len[i] )
                        dp[p][j][tmp|1] = max(dp[p][j][tmp|1], dp[p][j-1][k]+2*lf(i,j,k) ); //draw;
                    }
                    else if( j == 0 ){
                        if( caltop( m, k ) != len[i-1] ) continue;
                        dp[p][j][tmp] = max( dp[p][j][tmp], dp[p^1][m-1][k] );
                        dp[p][j][tmp|1] = max( dp[p][j][tmp|1], dp[p^1][m-1][k]+2*up(i,j,k) );
                    }
                    else{
                        if( caltop( j, k ) > len[i] ) continue;
                        if( calbtm( m-j, k ) > len[i-1] ) continue;
                        dp[p][j][tmp] = max(dp[p][j][tmp], dp[p][j-1][k]);// not draw
                        if( caltop( j, k ) != len[i] ){
                            dp[p][j][tmp|1] = max(dp[p][j][tmp|1], dp[p][j-1][k]+2*lf(i,j,k)+2*up(i,j,k) ); //draw;
                        }
                    }
                }
//                for( int q = 0; q <= maxbt; q ++ ){
//                    printf("%d dp[%d][%d][%d] = %d\n ", i, p, j, q, dp[p][j][q] );
//                }
//                printf("\n");
            }
        }
        //printf("%d\n", dp[0][2][8163]) ;
        int max1 = 0;
        for( i = 0; i < m; i ++ )
            for( j = 0; j <= maxbt; j ++ ){
            if( caltop( m, j ) == len[n] )
                max1 = max( max1, dp[n%2][i][j] );
            }
        printf("%d\n", max1);
    }
}
//  http://www.mamicode.com/info-detail-1066123.html
//  http://async.icpc-camp.org/d/124-jag-summer-2012-day-4-c-connect

Compiler output:
main.c:1:9: fatal error: cstdio: No such file or directory
    1 | #include<cstdio>
      |         ^~~~~~~~
compilation terminated.