POJ2817 状态DP

WordStack
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2605   Accepted: 892

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
题意:有个地方又理解错了,WA了两次,看样例吧:
2
abcdefg
aaafcee
结果:3
WA了两次看了discuss,看到这组样例才明白不是连续的对齐,只要对齐就可以
思路以及代码几乎都和上一道湖南大学的题一样~~
View Code
 1 #include <stdio.h>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 using namespace std;
 7 char ch[12][12];
 8 int dp[1<<12][12];
 9 int _map[12][12];
10 int n;
11 int max(int a,int b){return a>b?a:b;}
12 void get(int a,int b)
13 {
14     int i,j,k,s;
15     _map[a][b]=0;
16     for(int i=0;ch[a][i]!='\0';i++)
17        for(int j=0;ch[b][j]!='\0';j++)
18        {
19            k=s=0;
20            while(ch[a][i+k]!='\0'&&ch[b][j+k]!='\0')
21            {
22                if(ch[a][i+k]==ch[b][j+k])
23                s++;
24                k++;
25            }
26            _map[a][b]=max(_map[a][b],s);
27        }
28     _map[b][a]=_map[a][b];
29 }
30 int dfs(int state,int u)
31 {
32     if(dp[state][u]!=0)
33     return dp[state][u];
34     if(state==0)
35     return 0; //16ms
36     //return dp[0][u]=0; //32ms
37     for(int i=0;i<n;i++)
38     {
39         if(state&(1<<i))
40         {
41             int now=state^(1<<i);
42             dp[state][u]=max(dp[state][u],dfs(now,i)+_map[u][i]);
43         }
44     }
45     return dp[state][u];
46 }
47 int main()
48 {
49     while(scanf("%d",&n)!=EOF&&n)
50     {
51         getchar();
52         for(int i=0;i<n;i++)
53            scanf("%s",ch[i]);
54 
55         for(int i=0;i<n;i++)
56            for(int j=i;j<n;j++)
57            get(i,j);
58 
59         for(int i=0;i<(1<<n);i++)
60            for(int j=0;j<n;j++)
61            dp[i][j]=0;
62 
63         int res=0;
64         int state=(1<<n)-1;
65         for(int i=0;i<n;i++)
66         {
67             res=max(res,dfs(state^(1<<i),i));
68         }
69         printf("%d\n",res);
70     }
71 }

 

posted @ 2012-09-07 09:12  _sunshine  阅读(429)  评论(0编辑  收藏  举报