zoukankan      html  css  js  c++  java
  • poj 1159 (DP LCS)

    滚动数组 + LCS

     1 // File Name: 1159.cpp
     2 // Author: Missa_Chen
     3 // Created Time: 2013年07月08日 星期一 10时07分13秒
     4 
     5 #include <iostream>
     6 #include <string>
     7 #include <algorithm>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <cmath>
    11 #include <queue>
    12 #include <map>
    13 #include <stack>
    14 #include <set>
    15 #include <cstdlib>
    16 #include <vector>
    17 #include <time.h>
    18 
    19 using namespace std;
    20 
    21 const int maxn = 5e3 + 5;
    22 int n;
    23 char buf[maxn];
    24 int LCS(string s1, string s2)
    25 {
    26     int dp[2][maxn] = {0};
    27     int e = 0;
    28     for (int i = 1; i <= n; ++i)
    29     {
    30         e = 1 - e;
    31         for (int j = 1; j <= n; ++j)
    32             if (s1[i - 1] == s2[j - 1])
    33                 dp[e][j] = dp[1 - e][j - 1] + 1;
    34             else dp[e][j] = max (dp[1 - e][j], dp[e][j - 1]);
    35     }
    36     return dp[e][n];
    37 }
    38 int main()
    39 {
    40     while (~scanf("%d",&n))
    41     {
    42         scanf("%s", buf);
    43         string s, rev;
    44         s = rev = buf;
    45         reverse(rev.begin(), rev.end());
    46         printf("%d
    ", n - LCS(s, rev));
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    A. Difference Row
    B. Fixed Points
    命运
    Climbing Worm
    大学感想
    Constructing Roads
    lintcode605- Sequence Reconstruction- medium- airbnb google
    lintcode616- Course Schedule II- medium
    lintcode615- Course Schedule- medium
    lintcode127- Topological Sorting- medium
  • 原文地址:https://www.cnblogs.com/Missa/p/3178977.html
Copyright © 2011-2022 走看看