zoukankan      html  css  js  c++  java
  • L3-020 至多删三个字符 (30 分)(DP)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880

    学习地址:

    2018CCCC-L3-2:至多删三个字符(DP) - Mitsuha_的博客 - CSDN博客

    题目大意:给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串?

    具体思路:

    dp[i][j]表示前i个字符去除j个字符所形成的的不同的子串,然后对于每一次i的移动,

    dp[i][j+1]+=dp[i-1][j].前i个字符去除j+1个字符等于前i-1个字符去除j个字符的情况。

    dp[i][j]+=dp[i-1][j].前i个字符去除j个字符等于 前i-1个字符去除j个字符的情况。

    然后再就是去重的过程,对于cdabnaxy这个字符串,当我们去除abn和去除bna的时候,所形成的的字符都是一样的,暂时将第一个a的下标定位k,第二个a的下标定位j. 我们应该去除的情况是第一个a字符前面去除(j-3)个字符的情况.

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 # define ll long long
     4 # define inf 0x3f3f3f3f
     5 const int maxn = 2e6+10;
     6 ll dp[maxn][5];
     7 char str[maxn];
     8 int main()
     9 {
    10     scanf("%s",str+1);
    11     int len=strlen(str+1);
    12     dp[0][0]=1;
    13     for(int i=1;i<=len;i++){
    14     for(int j=0;j<4;j++){
    15     if(j<3)dp[i][j+1]=dp[i-1][j];
    16     dp[i][j]+=dp[i-1][j];
    17     for(int k=i-1;i-k<=j&&k>=1;k--){
    18     if(str[i]==str[k]){
    19     dp[i][j]-=dp[k-1][j-(i-k)];
    20     break;
    21     }
    22     }
    23     }
    24     }
    25     printf("%lld
    ",dp[len][0]+dp[len][1]+dp[len][2]+dp[len][3]);
    26     return 0;
    27 }
  • 相关阅读:
    css
    js -【 数组】判断一个变量是数组类型的几种方法
    【消灭代办】第2周
    【本周面试题】第2周
    【本周面试题】第1周
    【消灭代办】第1周
    echarts
    css
    js
    JS方法
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10585556.html
Copyright © 2011-2022 走看看