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活动条(actionbar)使用具体解释(一)
活动条(ActionBar)是Android3.0的重要更新之中的一个.ActionBar位于传统标题的位置,其主要提供了例如以下功能: >显示选项菜单的菜单项,即激昂菜单项显示 ...
- EasyDarwin流媒体云平台:EasyCamera开源摄像机接入海康威视摄像机实时视频
本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52755298 EasyCamera接收云平台实时 ...
- 九度OJ 1103:二次方程计算器 (解方程)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2804 解决:633 题目描述: 设计一个二次方程计算器 输入: 每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式. 输 ...
- HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件
HDFS被设计成能够在一个大集群中跨机器可靠地存储超大文件.它将每个文件存储成一系列的数据块,除了最后一个,所有的数据块都是同样大小的.为了容错,文件的所有数据块都会有副本.每个文件的数据块大小和副本 ...
- 如何解决Apache无法启动的问题
今天重装xampp后,一直无法启动Apache,提示以下错误: 22:36:54 [Apache] Attempting to start Apache app...22:36:54 [Apache] ...
- python基础知识踩点
1.注释 在python中,注释是以任何存在于#右侧的文字,其主要作用是写给程序读者看的笔记. 例如 单行注释 >>print("hello world") #这是一个 ...
- 人生苦短之Python枚举类型enum
枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用.当一个变量有几种可能的取值的时候,我们将它定义为枚举类型.在Python ...
- ArcGIS发布动态空间,并验证
发布 发布方法见视频. 验证 发布动态空间后,页面底部有 点进去后,使用如下语法验证. {"id": 0,"source": {"type" ...
- HTML layout高仿QQ GUI
1. [图片] QQ20130804162049.png 2. [代码]AAuto 代码 import win.ui;import web.layout;/*DSG{{*/winform = ...
- LoadRunner中两种录制模式的区别
决定我们成为什么样人的,不是我们的能力,而是我们的选择. ——<哈利-波特与密室> 一.先看看两种模式的设置和录制脚本的区别 设置HTML录制模式: 设置URL录制模式: HTML脚本: ...