状态压缩dp入门题
来源:互联网 发布:小企业电脑记账软件 编辑:程序博客网 时间:2024/05/16 01:39
#include<stdio.h>
#include<string.h>
#define max(a,b)(a>b?a:b)
int dp[1<<10+5][11];
int len[11][11];
int n;
char str[11][11];
int main()
{
int n,i,j,k,x,count;
int len1,len2,max;
while(scanf("%d",&n),n)
{
memset(len,0,sizeof(len));
for(i=0;i<n;i++)
scanf("%s",str[i]);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i!=j)
{
max=-1;//pay attention
len1=strlen(str[i]);
len2=strlen(str[j]);
for(k=0;k<len1;k++)
{
count=0;
for(x=0;x<len2&&(x+k)<len1;x++)
{
if(str[i][x+k]==str[j][x]) count++;
}
if(count>max) max=count;
}
if(max>len[i][j])
len[i][j]=len[j][i]=max;
}
}
}
memset(dp,0,sizeof(dp));
for(i=0;i<(1<<n);i++)
for(j=0;j<n;j++)
{
if(i&(1<<j))//if j is in the set of i
{
for(k=0;k<n;k++)
{
if(!(i&(1<<k)))//if k is not int the set of i,then process dp
dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]+len[j][k]);
}
}
}
max=-1;
for(i=0;i<(1<<n);i++)
for(j=0;j<n;j++)
if(dp[i][j]>max)
max=dp[i][j];
printf("%d\n",max);
}
return 0;
}
有个小小的注意点是,位运算的速度较快,所以合并集合时i|(1<<k)比i+(1<<k) 要略优
WordStack
Time Limit:1000MS Memory Limit:65536K
Total Submit:12 Accepted:11
Description
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
Hint
Note: One possible arrangement yielding this score is:
aaa
abc
bcd
cde
bfcde
Source
Mid-Atlantic 2005
- 状态压缩DP入门题
- 状态压缩DP入门题
- 状态压缩dp入门题
- 状态压缩DP入门题
- 状态压缩DP 入门
- 状态压缩DP入门
- 状态压缩dp入门
- 状态压缩DP入门
- 状态压缩DP入门
- 【状态压缩DP】【POJ3254】【POJ1185】 入门题
- poj 3254 状态压缩dp入门题
- 状态压缩dp入门题 poj3254
- 状态压缩dp入门题目
- 状态压缩DP(入门)
- 【状态压缩dp 入门】POJ
- 【状态压缩】---状态压缩dp第一题
- 状态压缩DP 简单入门题 11题
- 状态压缩DP poj 2817 WordStack 入门题
- Java基础之003-Java语法
- 视频: 日本超酷的超导磁场赛道演示
- 食物链总结
- 霍夫曼树的应用
- dp 有一些细节要注意
- 状态压缩dp入门题
- 树形dp入门题目
- Matrix Chain Multiplication 模拟栈
- stars 第一个树状数组,水过~
- 树状数组的离散化 Ultra-QuickSort
- Color the ball 树状数组水之
- poj 1656
- 继续二维树状数组 pku1195
- hdu 1698 线段树 just a “trick” *_*