码迷,mamicode.com
首页 > 其他好文 > 详细

UVA11584-Partitioning by Palindromes(动态规划基础)

时间:2018-10-06 23:40:07      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:you   letter   english   相对   other   一个   比较   ==   nbsp   

Problem UVA11584-Partitioning by Palindromes

Accept: 1326  Submit: 7151
Time Limit: 3000 mSec

技术分享图片 Problem Description

技术分享图片

技术分享图片

 

 

 

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the ?rst line of the input. Each test case consists of two lines. In the ?rst line, a character string of length n (1 ≤ n ≤ 5,000) that is the color information of the cars in one lane before merging is given. In the second line, a character string of length m (1 ≤ m ≤ 5,000) that is the color information of the cars in the other lane is given. Every color is represented as an uppercase letter in English, hence the number of colors is less than or equal to 26.

 

技术分享图片 Output

Your program is to read from standard input. Print exactly one line for each test case. The line should contain the sum of color lengths after merging the cars in the two lanes optimally as described above. The following shows sample input and output for two test cases.
 

技术分享图片 Sample Input

2
AAABBCY
ABBBCDEEY
GBBY
YRRGB
 

技术分享图片 Sample Output

10

12

 

题解:这个题真是醉了,自己想状态,总有后效性,想了很久还是看了lrj的思路,恍然大悟,还是很有启发性的,局部有后效性,但是整体上没有,不失为以后考虑问题的一个思路,本以为这个题已经没啥说的了,写的时候才发现细节太多了,if 、else写到恶心,还一直有不对的地方,后来在不断调试过程中发现问题主要出现在只出现在一个字符串中的字符上,想出了一个很好的解决方案,就是将第一次出现的地方初始化为INF,没出现就代表着出现在无穷远处,将最后出现的地方初始化为0,没出现就代表着在最前面结束,有了这个初始化就好写很多,调一调就好,dp相对转移起来还是比较简单的,方程见代码。

 

 1 #include <bits/stdc++.h>
 2 //#define DE
 3 
 4 using namespace std;
 5 
 6 const int maxn = 5000 + 10;
 7 const int kind = 256;
 8 const int INF = 0x3f3f3f3f;
 9 
10 char a[maxn], b[maxn];
11 int num[maxn][maxn], dp[maxn][maxn];
12 int st[2][kind], tail[2][kind];
13 int alen, blen;
14 
15 void pre_management(int p, char *str) {
16     int len = strlen(str + 1);
17     for (int i = 1; i <= len; i++) {
18         if (st[p][str[i]] == INF) st[p][str[i]] = i;
19     }
20     for (int i = len; i >= 1; i--) {
21         if (tail[p][str[i]] == 0) tail[p][str[i]] = i;
22     }
23 }
24 
25 void init() {
26     memset(st, INF, sizeof(st));
27     memset(tail, 0, sizeof(tail));
28     pre_management(0, a); pre_management(1, b);
29 
30     int cnt = 0;
31     for (int i = 0; i <= alen; i++) {
32         for (int j = 0; j <= blen; j++) {
33             if (i) {
34                 num[i][j] = num[i - 1][j];
35                 if (i == st[0][a[i]] && j < st[1][a[i]]) num[i][j]++;
36                 if (i == tail[0][a[i]] && j >= tail[1][a[i]]) num[i][j]--;
37             }
38             if (j) {
39                 num[i][j] = num[i][j - 1];
40                 if (j == st[1][b[j]] && i < st[0][b[j]]) num[i][j]++;
41                 if (j == tail[1][b[j]] && i >= tail[0][b[j]]) num[i][j]--;
42             }
43         }
44     }
45 }
46 
47 void solve() {
48     dp[0][0] = 0;
49     for (int i = 0; i <= alen; i++) {
50         for (int j = 0; j <= blen; j++) {
51             if (!i && !j) continue;
52             dp[i][j] = INF;
53             if (i) {
54                 dp[i][j] = min(dp[i - 1][j] + num[i][j], dp[i][j]);
55             }
56             if (j) {
57                 dp[i][j] = min(dp[i][j - 1] + num[i][j], dp[i][j]);
58             }
59         }
60     }
61     printf("%d\n", dp[alen][blen]);
62 }
63 
64 int main()
65 {
66     //freopen("input.txt", "r", stdin);
67     int iCase;
68     scanf("%d", &iCase);
69     while (iCase--) {
70         scanf("%s%s", a + 1, b + 1);
71         alen = strlen(a + 1), blen = strlen(b + 1);
72         init();
73         solve();
74 #ifdef DE
75         for (int i = 1; i <= alen; i++) {
76             for (int j = 1; j <= blen; j++) {
77                 printf("num[%d][%d]: %d ", i, j, num[i][j]);
78             }
79             printf("\n");
80         }
81         for (int i = 1; i <= alen; i++) {
82             for (int j = 1; j <= blen; j++) {
83                 printf("%d ", num[i][j]);
84             }
85             printf("\n");
86         }
87 #endif // DE
88     }
89     return 0;
90 }

 

UVA11584-Partitioning by Palindromes(动态规划基础)

标签:you   letter   english   相对   other   一个   比较   ==   nbsp   

原文地址:https://www.cnblogs.com/npugen/p/9748981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!