hdu4545--暴力/水dp--LCS/LIS真的是个好东西

数据不大 可以暴力做..

不想提这该死的暴力了  晓爷 你出来 保证不打死你!

这题 也可以用Lcs来做 相比于以往的最裸的lcs 就是多了个 hash配对

只要多添加个条件就可以了

既然是Lcs 那么就可以用 滚动数组来优化下

因为dp[i][j] 这一状态 只与dp[i-1][j] dp[i-1][j-1] dp[i][j-1]这3个状态有关 那么即只和这一行和前一行有关系 我们可以只开一个2维数组 第一维的大小为2就足够了 存储当前行与上一行的值 以后就不断转移 覆盖

还可以更节约内存  只通过2个中间变量 来传递状态的值

var----dp[i-1][j-1]      dp[j-1] ----- dp[i][j-1]     dp[j] ----- dp[i-1][j]

暴力的代码 不贴了 搞的心好累.

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int size = 1010;
 7 char ss[size];
 8 char tt[size];
 9 int dp[size];
10 bool hash[30][30];
11
12 int main()
13 {
14     int t , m , len1 , len2 , var , temp;
15     char x , y;
16     cin >> t;
17     for( int k = 1 ; k<=t ; k++ )
18     {
19         cin >> (ss+1) >> (tt+1);
20         cin >> m;
21         memset( dp , 0 , sizeof(dp) );
22         memset( hash , false , sizeof(hash) );
23         len1 = strlen(ss+1);
24         len2 = strlen(tt+1);
25         while( m-- )
26         {
27             cin >> x >> y;
28             hash[x-‘a‘][y-‘a‘] = true;
29         }
30         for( int i = 1 ; i<=len1 ; i++ )
31         {
32             var = 0;
33             for( int j = 1 ; j<=len2 ; j++ )
34             {
35                 temp = dp[j];
36                 if( ss[i] == tt[j] || hash[ tt[j]-‘a‘ ][ ss[i]-‘a‘ ] )
37                 {
38                     dp[j] = var + 1;
39                 }
40                 else
41                 {
42                     dp[j] = max( dp[j-1] , dp[j] );
43                 }
44                 var = temp;
45             }
46         }
47         cout << "Case #" << k << ": ";
48         if( dp[len2] == len1 )
49             cout << "happy" << endl;
50         else
51             cout << "unhappy" << endl;
52     }
53     return 0;
54 }

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int size = 1010;
 7 char ss[size];
 8 char tt[size];
 9 int dp[size][size];
10 bool hash[30][30];
11
12 int main()
13 {
14     int t , m , len1 , len2;
15     char x , y;
16     cin >> t;
17     for( int k = 1 ; k<=t ; k++ )
18     {
19         cin >> (ss+1) >> (tt+1);
20         cin >> m;
21         memset( dp , 0 , sizeof(dp) );
22         memset( hash , false , sizeof(hash) );
23         len1 = strlen(ss+1);
24         len2 = strlen(tt+1);
25         while( m-- )
26         {
27             cin >> x >> y;
28             hash[x-‘a‘][y-‘a‘] = true;
29         }
30         for( int i = 1 ; i<=len1 ; i++ )
31         {
32             for( int j = 1 ; j<=len2 ; j++ )
33             {
34                 if( ss[i] == tt[j] || hash[ tt[j]-‘a‘ ][ ss[i]-‘a‘ ] )
35                 {
36                     dp[i][j] = dp[i-1][j-1] + 1;
37                 }
38                 else
39                 {
40                     dp[i][j] = max( dp[i-1][j] , dp[i][j-1] );
41                 }
42             }
43         }
44         cout << "Case #" << k << ": ";
45         if( dp[len1][len2] == len1 )
46             cout << "happy" << endl;
47         else
48             cout << "unhappy" << endl;
49     }
50     return 0;
51 }

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int size = 1010;
 7 char ss[size];
 8 char tt[size];
 9 int dp[2][size];
10 bool hash[30][30];
11
12 int main()
13 {
14     int t , m , len1 , len2;
15     char x , y;
16     cin >> t;
17     for( int k = 1 ; k<=t ; k++ )
18     {
19         cin >> (ss+1) >> (tt+1);
20         cin >> m;
21         memset( dp , 0 , sizeof(dp) );
22         memset( hash , false , sizeof(hash) );
23         len1 = strlen(ss+1);
24         len2 = strlen(tt+1);
25         while( m-- )
26         {
27             cin >> x >> y;
28             hash[x-‘a‘][y-‘a‘] = true;
29         }
30         for( int i = 1 ; i<=len1 ; i++ )
31         {
32             for( int j = 1 ; j<=len2 ; j++ )
33             {
34                 if( ss[i] == tt[j] || hash[ tt[j]-‘a‘ ][ ss[i]-‘a‘ ] )
35                 {
36                     dp[i%2][j] = dp[(i-1)%2][j-1] + 1;
37                 }
38                 else
39                 {
40                     dp[i%2][j] = max( dp[(i-1)%2][j] , dp[i%2][j-1] );
41                 }
42             }
43         }
44         cout << "Case #" << k << ": ";
45         if( dp[len1%2][len2] == len1 )
46             cout << "happy" << endl;
47         else
48             cout << "unhappy" << endl;
49     }
50     return 0;
51 }

永远 最容易理解的 是 消耗最大的-

today:

  我也曾经憧憬过 后来没结果

  我也曾经做梦过 后来更寂寞

  假装我很怀旧

时间: 2024-11-06 06:33:45

hdu4545--暴力/水dp--LCS/LIS真的是个好东西的相关文章

hdu 1423(LCS+LIS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据之外都需要空行.. 其余的会LCS打印路径就行了. ///公共最长上升子序列 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <

poj1458——dp,lcs

poj1458——dp,lcs Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40529   Accepted: 16351 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence

hduoj1159 dp,lcs

hduoj1159  dp,lcs Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25570    Accepted Submission(s): 11363 Problem Description A subsequence of a given sequence is the given seq

动态规划(DP),类似LIS,FatMouse&#39;s Speed

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 解题报告: 1.首先按照weight从小到大排列,weight相同的按照speed从大到小排列; 2.Count[i]表示到第i个老鼠时,所求的最长“速度递减”子序列的长度: 3.path[i]=j是题目的关键,记录在Count[i]=Count[j]时,即最长“速度递减”子序列最后一个老鼠的前一只老鼠的位置 4.递归输出id void output(in

hdu 2571 命运(水DP)

题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j),可以走到(i,k*j) 问取走的最大和是多少. 思路: 水DP...边界的初始化要考虑.(因为有负数). 代码: int n,m; int a[30][1005]; int dp[30][1005]; int main(){ int T; cin>>T; while(T--){ cin>&g

hdu5074 Hatsune Miku 2014鞍山现场赛E题 水dp

http://acm.hdu.edu.cn/showproblem.php?pid=5074 Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 243 Problem Description Hatsune Miku is a popular vi

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

hoj_10001_朴素DP(LIS)

Longest Ordered Subsequence Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: 1937, Accepted users: 1621 Problem 10001 : No special judgement Problem description A numeric sequence of ai is ordered if a1 < a2 <