WordStack
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3263 | Accepted: 1172 |
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.
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 .
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[i][j]表示当前放置状态为i,且现在要放第j个串时的最大值(i & (1 << j)为0),当然串与串之间的匹配是要去计算的,不过串的长度很短。
状态压缩dp,dp[i][j]表示当前放置状态为i,且现在要放第j个串时的最大值(i & (1 << j)为0),当然串与串之间的匹配是要去计算的,不过串的长度很短。
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1100][15];
int match[15][15];
char str[15][15];
int max_match(char a[], char b[])
{
int cnt = 0;
int x = 0;
while (a[x] != '\0')
{
int s = x;
int t = 0;
int ans = 0;
while (a[s] != '\0' && b[t] != '\0')
{
if (a[s] == b[t])
{
ans++;
}
s++;
t++;
}
cnt = max(ans, cnt);
x++;
}
return cnt;
}
int main()
{
int n;
while (~scanf("%d", &n), n > 0)
{
memset (dp, 0, sizeof(dp));
for (int i = 0; i < n; ++i)
{
scanf("%s", str[i]);
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
match[i][j] = max_match(str[i], str[j]);
match[j][i] = max_match(str[j], str[i]);
match[i][j] = match[j][i] = max(match[i][j], match[j][i]);
}
}
for (int i = 0; i < (1 << n); ++i)
{
for (int j = 0; j < n; ++j)
{
if (i & (1 << j))//第j位是0
{
continue;
}
for (int k = 0; k < n; ++k)
{
if (j == k)
{
continue;
}
if (!(i & (1 << k)))
{
continue;
}
dp[i ^ (1 << j)][j] = max(dp[i ^ (1 << j)][j], dp[i][k] + match[k][j]);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i)
{
ans = max(ans, dp[(1 << n) - 1][i]);
// printf("%d\n", dp[(1 << n) - 1][i]);
}
printf("%d\n", ans);
}
return 0;
}