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[][],w[][],dp[<<][];
char ch[][];
int count(int x,int y){
memset(f,,sizeof(f));
int len1=strlen(ch[x]+);
int len2=strlen(ch[y]+);
int res=;
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
int cnt=;
for(int k=;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(){
scanf("%d",&n);
memset(w,,sizeof(w));
memset(dp,,sizeof(dp));
if(n==)return ;
for(int i=;i<=n;i++)
scanf("%s",ch[i]+);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)continue;
w[i][j]=count(i,j);
}
}
for(int i=;i<(<<n);i++){//枚举单词的所有状态
for(int j=;j<=n;j++){//枚举目前最后一行可能的单词
if(i&(<<(j-))){
for(int k=;k<=n;k++)
if(i^(<<(k-))){
int to=i+(<<(k-));
dp[to][k]=max(dp[to][k],dp[i][j]+w[j][k]);
}
} }
}
int ans=;
for(int i=;i<=n;i++)
ans=max(ans,dp[(<<n)-][i]);
printf("%d\n",ans);
}
}

hoj2188 WordStack的更多相关文章

  1. POJ2817 WordStack(状压DP)

    题目给几个字符串,可以给它们添加前导空格,然后排列,计算每一个字符串和前一个字符串相同非空格字符相等的个数,求可能的最大个数. 状态DP: d[S][i][j]表示已经用的字符串集合S且排列的最后一个 ...

  2. [HDU 4433]locker[DP]

    题意: 给出密码做的现状和密码, 每次可以移动连续的最多3列, 向上或向下, 求将密码调出来所需要的最少步数. 思路: 首先应看出,恢复的过程中, 调每一位的时间顺序是不影响的, 不妨就从左到右一位位 ...

  3. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

  4. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  5. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...

随机推荐

  1. iOS开发工具篇-AppStore统计工具

    本文转载至 http://mobile.51cto.com/hot-418183.htm 随着iOS开发的流行,针对iOS开发涉及的方方面面,早有一些公司提供了专门的解决方案或工具.这些解决方案或工具 ...

  2. 九度OJ 1098:字母统计 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3720 解决:1809 题目描述: 输入一行字符串,计算其中A-Z大写字母出现的次数 输入: 案例可能有多组,每个案例输入为一行字符串. 输 ...

  3. 利用ES6中的Proxy和Reflect 实现简单的双向数据绑定

    利用ES6中的Proxy (代理) 和 Reflect 实现一个简单的双向数据绑定demo. 好像vue3也把 obj.defineProperty()  换成了Proxy+Reflect. 话不多说 ...

  4. extjs grid renderer参数用法

    今天在导出EXT的二维时老是报错,追进去看是renderer : function(value)的参数不对,经过一番研究,未免以后遇到再次浪费时间,记录一下. var cm = new Ext.gri ...

  5. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  6. poj2586 Y2K Accounting Bug —— 枚举

    链接:http://poj.org/problem?id=2586 题意:大意是一个公司在12个月中,或固定盈余s,或固定亏损d.但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(和 ...

  7. BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  8. input处理函数

    input处理函数是潜在的影响你app性能的问题,他们可以阻止帧的形成,并且可以造成多余的亦或不必要的layout的工作. 避免长时间运行input handler:它们会阻塞scroll 不要在in ...

  9. C语言教学杂记——字母排序

    一个人在被告诉一个问题应该怎么被解决后,而且亲身试验效果OK后,一旦遇到类似的问题,就会条件反射般直接拿这个方法来用了.很少会去想为什么要用这个方法,会不会有什么隐患,还有没有别的方法呢,等等这些问题 ...

  10. SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

    Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it ...