poj 2817 WordStack

Description

As editor of a small-town newspaper, you know that a substantial number of your readers enjoy the daily word games that you publish, but that some are getting tired of the conventional crossword puzzles and word jumbles that you have been buying for years. You decide to try your hand at devising a new puzzle of your own.

Given a collection of N words, find an arrangement of the words that divides them among N lines, padding them with leading spaces to maximize the number of non-space characters that are the same as the character immediately above them on the preceding line. Your score for this game is that number.

Input

Input data will consist of one or more test sets.

The first line of each set will be an integer N (1 <= N <= 10) giving the number of words in the test case. The following N lines will contain the words, one word per line. Each word will be made up of the characters 'a' to 'z' and will be between 1 and 10 characters long (inclusive).

End of input will be indicated by a non-positive value for N .

Output

Your program should output a single line containing the maximum possible score for this test case, printed with no leading or trailing spaces.

Sample Input

5 
abc 
bcd 
cde 
aaa 
bfcde 
0

Sample Output

8

Hint

Note: One possible arrangement yielding this score is:
aaa 

abc 

 bcd

  cde 

bfcde

Source


简单状态dp

题意就是给10个字符串,让你求一个顺序,使得相邻两个串(可以错位)相同位置的字符和最多……我也说不清楚,看Hint就很明显了。

但是要注意abc和adc算2个重复的字符……在这里wa了一次。

上代码吧

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char map[105][105];
int dp[(1<<11)+2][14];
int val[15][15];

int Count(char *p,char *q)
{
    int i,j,n,m,cnt,ret,k;
    ret=0;
    n=strlen(p);
    m=strlen(q);
    for (i=0;i<n;i++)
    {
        for (j=0;j<m;j++)
        {
            cnt=0;
            for (k=0;;k++)
            {
                if (i+k>=n || j+k>=m) break;
                if (p[i+k]==q[j+k]) cnt++;
            }
            ret=max(ret,cnt);
        }
    }
    return ret;

}

int main()
{
    int i,j,p,n,k,ret;
    while(1)
    {
        scanf("%d",&n);
        if (n==0) break;
        for (i=0;i<n;i++)
        {
            scanf("%s",map[i]);
        }
        for (i=0;i<n;i++)
        {
            for (j=i+1;j<n;j++)
            {
                val[i][j]=val[j][i]=Count(map[i],map[j]);
            }
        }
        memset(dp,-1,sizeof(dp));
        for (i=1;i<(1<<n);i++)
        {
            for (j=0;j<n;j++)
            {
                if (i==(1<<j)) break;
            }
            if (j<n)
            {
                dp[i][j]=0;
            }
            for (j=0;j<n;j++)
            {
                if ((i & (1<<j))!=0) continue;
                p=(i | (1<<j));
                for (k=0;k<n;k++)
                {
                    if ((i & (1<<k))==0) continue;
                    if (dp[i][k]==-1) continue;
                    dp[p][j]=max(dp[p][j],dp[i][k]+val[j][k]);
                }
            }
        }
        ret=0;
        for (i=0;i<n;i++)
        {
            ret=max(ret,dp[(1<<n)-1][i]);
        }
        printf("%d\n",ret);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值