100道动态规划——2 UVA 1625 Color Length DP 转移的代价稍微难写一点 顺便贴了刘汝佳的代码

                 这道题是第二道100道动态规划之中的,老实说一开始想到了定义这个状态dp[i][j]代表第一行用了i个,第二行用了j个这样。可是想不到好办法去写转移方程。。。后来实在想不出来了,好吧,看书。发现书上用一种很巧妙的办法处理的求转移的代价,那就是每一次+上已经开始但还未结束的字符个数。顿时就豁然开朗呀。马上动手

                 但是依然不会写。。。发现自己写出来的总是有问题。。想了好久处理这个地方。。好吧,最好还是去翻了代码仓库,刘汝佳的代码用了滚动数组的写法,其实大致思路和我差不多(捂脸),但我就是转移的代价不会写。。我觉得应该是我分析问题还不够透彻的缘故。以下是我的代码

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int T,t,be[2][26],ed[2][26],cnt[2][5010],dp[2][5010],len[2];
char str[2][5010];

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%s%s",str[0]+1,str[1]+1);
        len[0]=strlen(str[0]+1),len[1]=strlen(str[1]+1);
        memset(be,0x3f,sizeof be);
        memset(ed,0,sizeof ed);
        for(int i=0;i<2;++i){
            for(int j=1;str[i][j];++j){
                if(be[i][str[i][j]-'A']==0x3f3f3f3f)
                    be[i][str[i][j]-'A']=j;
                ed[i][str[i][j]-'A']=j;
            }
        }

        for(int i=0;i<=len[0];++i){
            for(int j=0;j<=len[1];++j){
                if(!i&&!j)
                    continue;

                dp[t][j]=min((i)?(dp[t^1][j]+cnt[t^1][j]):0x3f3f3f3f,(j)?(dp[t][j-1]+cnt[t][j-1]):0x3f3f3f3f);

                if(i){
                    cnt[t][j]=cnt[t^1][j];

                    if(i==be[0][str[0][i]-'A']&&j<be[1][str[0][i]-'A'])++cnt[t][j];
                    if(i==ed[0][str[0][i]-'A']&&j>=ed[1][str[0][i]-'A'])--cnt[t][j];
                }
                else if(j){
                    cnt[t][j]=cnt[t][j-1];

                    if(j==be[1][str[1][j]-'A']&&i<be[0][str[1][j]-'A'])++cnt[t][j];
                    if(j==ed[1][str[1][j]-'A']&&i>=ed[0][str[1][j]-'A'])--cnt[t][j];
                }
            }
            t^=1;
        }
        printf("%d\n",dp[t^1][len[1]]);
        //t=0;
        memset(dp,0,sizeof dp);
        memset(cnt,0,sizeof cnt);
    }
    return 0;
}


再把刘汝佳的代码也贴上来

// UVa1625 Color Length
// Rujia Liu

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 5000 + 5;
const int INF = 1000000000;

char p[maxn], q[maxn]; // starts from position 1
int sp[26], sq[26], ep[26], eq[26]; // sp[i] start positions of character i in p
int d[2][maxn], c[2][maxn]; // c[i][j]: how many "incomplete" colors in the mixed sequence

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    scanf("%s%s", p+1, q+1);

    int n = strlen(p+1);
    int m = strlen(q+1);
    for(int i = 1; i <= n; i++) p[i] -= 'A';
    for(int i = 1; i <= m; i++) q[i] -= 'A';

    // calculate s and e
    for(int i = 0; i < 26; i++) { sp[i] = sq[i] = INF; ep[i] = eq[i] = 0; }
    for(int i = 1; i <= n; i++) {
      sp[p[i]] = min(sp[p[i]], i);
      ep[p[i]] = i;
    }
    for(int i = 1; i <= m; i++) {
      sq[q[i]] = min(sq[q[i]], i);
      eq[q[i]] = i;
    }

    // dp
    int t = 0;
    memset(c, 0, sizeof(c));
    memset(d, 0, sizeof(d));
    for(int i = 0; i <= n; i++){
      for(int j = 0; j <= m; j++){
        if(!i && !j) continue;

        // calculate d
        int v1 = INF, v2 = INF;
        if(i) v1 = d[t^1][j] + c[t^1][j]; // remove from p
        if(j) v2 = d[t][j - 1] + c[t][j - 1]; // remove from q
        d[t][j] = min(v1, v2);

        // calculate c
        if(i) {
          c[t][j] = c[t^1][j];
          if(sp[p[i]] == i && sq[p[i]] > j) c[t][j]++;
          if(ep[p[i]] == i && eq[p[i]] <= j) c[t][j]--;
        } else if(j) {
          c[t][j] = c[t][j - 1];
          if(sq[q[j]] == j && sp[q[j]] > i) c[t][j]++;
          if(eq[q[j]] == j && ep[q[j]] <= i) c[t][j]--;
        }
      }
      t ^= 1;
    }
    printf("%d\n", d[t^1][m]);
  }
  return 0;
}


恩,还是要继续加油呀。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值