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[][],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的更多相关文章
- POJ2817 WordStack(状压DP)
题目给几个字符串,可以给它们添加前导空格,然后排列,计算每一个字符串和前一个字符串相同非空格字符相等的个数,求可能的最大个数. 状态DP: d[S][i][j]表示已经用的字符串集合S且排列的最后一个 ...
- [HDU 4433]locker[DP]
题意: 给出密码做的现状和密码, 每次可以移动连续的最多3列, 向上或向下, 求将密码调出来所需要的最少步数. 思路: 首先应看出,恢复的过程中, 调每一位的时间顺序是不影响的, 不妨就从左到右一位位 ...
- OJ题目分类
POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 学习笔记:状态压缩DP
我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...
随机推荐
- Android 属性动画ObjectAnimator和ValueAnimator讲解
区别: ObjectAnimator 是直接对某个view进行更改. ValueAnimator 根据 TimeInterpolator 在不断产生相应的数据,来传进view ,view自己做改变. ...
- c++中class与struct的区别
0. 结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型. 1.结构在堆栈中创建,是值类型,而类是引用类型.每当需要一种 ...
- Promise 源码分析
前言 then/promise项目是基于Promises/A+标准实现的Promise库,从这个项目当中,我们来看Promise的原理是什么,它是如何做到的,从而更加熟悉Promise 分析 从ind ...
- 异步模式模式Future(结合Callable可以获取线程返回结果)
submit 和 excute是有啥区别 如果有这样的需求: 多线程实现下载,提高效率. 不论是Thread类还是Runnable接口重写run方法,有个特点就是没有返回值~~~~~~ 我都主线程 如 ...
- html5--3.21 课程小结与其他新增元素
html5--3.21 课程小结与其他新增元素 学习要点 了解新增的input属性pattern 其他几个新增元素(非表单中元素,但是也放在这里讲解) 新增的input属性pattern:设定输入类型 ...
- bzoj 2655 calc —— 拉格朗日插值
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2655 先设 f[i][j] 表示长度为 i 的序列,范围是 1~j 的答案: 则 f[i][ ...
- POI实现DOC/DOCX转HTML
1.使用HWPF处理DOC public class DocToHtml { private static final String encoding = "UTF-8"; pub ...
- ubuntu 使用命令行清空回收站
sudo rm -rf ~/.local/share/Trash/*
- Keil BUG 导致读字库时,无法显示某些汉字解决办法
原因在于:KEIL C51 的一个汉字BUG,keil c51在编译的时候会将0xfd的字符(有些汉字含该字符的内码)过滤, 最佳解决方案:打补丁,用 晓奇工作室出的补丁 cckeilvxx.exe ...
- 五、怎样修改oracle某个用户的密码
1.键入命令:sqlplus / as sysdba 2.在sqlplus窗口执行命令: alter user you_username identified by you_password;