hoj2188 WordStack

WordStack

My Tags   (Edit)
  Source : Mid-Atlantic 2005
  Time limit : 5 sec   Memory limit : 32 M

Submitted : 274, Accepted : 143

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

One possible arrangement yielding this score is:

aaa
abc
 bcd
  cde
bfcde

题意:给出N个单词,要你重新排列它们,可以在它们前面加上空格,当一行的单词的有x个字母与上一行的单词相对应的位置的字母一样,那么你能获得x分,问最最多能获得多少分

/*
    因为N最大只有10,然后又要考虑选择的先后顺序,所以我们想到用状态压缩,我们用dp[i][j] 表示状态为i,目前最后一行为j单词的最大分数
    于是状态转移方程就是 dp[i][j] = max(dp[i][j],dp[i-x][k]+w[k][j])
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,f[11][11],w[11][11],dp[1<<11][11];
char ch[11][11];
int count(int x,int y){
    memset(f,0,sizeof(f));
    int len1=strlen(ch[x]+1);
    int len2=strlen(ch[y]+1);
    int res=0;
    for(int i=1;i<=len1;i++){
        for(int j=1;j<=len2;j++){
            int cnt=0;
            for(int k=0;k+i<=len1&&k+j<=len2;k++)
                if(ch[x][i+k]==ch[y][j+k])++cnt;
            res=max(res,cnt);
        }
    }
    return res;
}
int main(){
    //freopen("Cola.txt","r",stdin);
    while(1){
        scanf("%d",&n);
        memset(w,0,sizeof(w));
        memset(dp,0,sizeof(dp));
        if(n==0)return 0;
        for(int i=1;i<=n;i++)
            scanf("%s",ch[i]+1);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j)continue;
                w[i][j]=count(i,j);
            }
        }
        for(int i=0;i<(1<<n);i++){//枚举单词的所有状态 
            for(int j=1;j<=n;j++){//枚举目前最后一行可能的单词 
                if(i&(1<<(j-1))){
                    for(int k=1;k<=n;k++)
                        if(i^(1<<(k-1))){
                            int to=i+(1<<(k-1));
                            dp[to][k]=max(dp[to][k],dp[i][j]+w[j][k]);
                        }
                }
                
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            ans=max(ans,dp[(1<<n)-1][i]);
        printf("%d
",ans);
    }
}
原文地址:https://www.cnblogs.com/thmyl/p/7339579.html