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问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...
随机推荐
- FormsAuthentication 在asp.net MVC中的应用
说明:开发环境 vs2012 asp.net mvc4 c# 项目结构: 1.开发步骤 1.1 创建项目 打开vs2012 开发环境 “文件”--“新建”--“项目” 选择asp.net mvc项目类 ...
- c# winform窗体间的传值
说明:本文讲解两个窗体之间的传值,主要用到两个窗体,form1,form2 1.在form1窗体单击按钮,打开窗体form2,然后把form2中文本框的值传递给form1 form1中的代码: usi ...
- wifi 协议栈的历史的总结
google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- 【小程序】bindconfirm点击小键盘触发事件、focus自动获取焦点
最近在写小程序,项目要求写一个搜索框,在进入页面时就触发input的事件,调出键盘,点小键上的搜索按钮 就触发搜索事件,分享一下. bindconfirm 是点击小键盘上的搜索按钮就触发要执行的方法 ...
- hadoop 常用命令总结
1. 查看集群资源信息 hdfs dfsadmin -report 2. 启动一个mapreduce任务, hadoop jar /opt/hadoop/share/hadoop/tools/lib/ ...
- codeforces 466C. Number of Ways 解题报告
题目链接:http://codeforces.com/problemset/problem/466/C 题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种. ...
- 盈创动力之 JS校验方法
var IS_NULL = 128; // 10000000var IS_FULL = 64; // 01000000var IS_HALF = 32; // 00100000var IS_ASCII ...
- git 错误 Reinitialized existing Git repository in /**/***/ 和refusing to merge unrelated histories
报错一: 这句话的意思是 在路径 /Users/jackma/Downloads/lotteryTicket 2/.git/ 现有的Git存储库初始化 ➜ lotteryTicket 2 git:(m ...
- No result defined for action cn.crm.action.LinkManAction and result input
这是struts2的一个拦截器报的错误,当你的form中的数据有问题,比如说<input type="text" name="receiverLoginID&quo ...