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问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...
随机推荐
- 【BZOJ3782】上学路线 组合数+容斥+CRT
[BZOJ3782]上学路线 Description 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不 ...
- 【BZOJ1110】[POI2007]砝码Odw 贪心
[BZOJ1110][POI2007]砝码Odw Description 在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作.公司有一些固定容量的容器可以装这些砝码 ...
- HDU4848 Wow! Such Conquering! —— dfs + 剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4848 题解: 一开始读错题目.以为每个点只能访问一遍.其实只要每个点都有被访问就可以了. 首先是用弗洛 ...
- C语言中的排序算法--冒泡排序,选择排序,希尔排序
冒泡排序(Bubble Sort,台湾译为:泡沫排序或气泡排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...
- 【转】看C++文档的小知识
转载:http://www.ggv.com.cn/forum/clib/ctype/isspace.html 函数isspace 原型:extern int isspace(int c); 用法 ...
- Linux GCC常用命令学习
1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...
- 安装asterisk以及asterisk-gui
asterisk的安装在ubuntu上自我感觉还是很方便的,虽然也会遇到一些小的问题.下面是本人遇到的 一些问题和解决方法. 1>在ubuntu10.04上安装aste ...
- [HNOI 2010] 弹飞绵羊
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2002 [算法] LCT动态维护森林连通性 时间复杂度 : O(NlogN ^ 2) ...
- AutoIt:应用WMI接口,打印远程机器安装的所有应用程序
Dim $computer ="computer name" Dim $Remoteuser ="Login account" Dim $Remotepass ...
- Struts过滤器
StrutsPrepareAndExecuteFilter过滤器其实是包含2部分的 StrutsPrepareFilter:做准备 StrutsExecuteFilter:进入Struts2的核心处理 ...