zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP

    题目链接:http://codeforces.com/contest/132/problem/C


    C. Logo Turtle
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").

    You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?

    Input

    The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F".

    The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list.

    Output

    Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.

    Examples
    input
    FT
    1
    
    output
    2
    
    input
    FFFTFFF
    2
    
    output
    6
    
    Note

    In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.

    In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.

    题解:

    方法一(四维dp):

    1.dp[i][j][dir][dis]表示:执行到第i个指令,修改了j个指令,前进方向为dir,且到达了dis的地方的情况是否存在。其值为0或1。

    2.枚举已有状态,推出下一步状态。(与常见的dp不同,常见的dp为枚举可能的状态,然后看他能从那些状态转移过来)。

    3.由于结束点可能在左边,即距离为负数,为了防止溢出,将起始点往右移100。

    类似的做法的题:http://blog.csdn.net/dolfamingo/article/details/73903530



    易错点:

    写成dp[i][j][dir][dis] 或者dp[i][j][dis][dir]都可以,但是写成dp[dir][i][j][dis] 或者dp[dis][i][j][dir]等等就不行了,因为枚举的顺序不对。递推必须自底向上,如果“底”都没推出来,那“上”自然也推不出来了。



    学习之处:

    一:目前了解到的递推式DP有两种:

    1.当前状态(可能不存在)能从哪些状态转移过来。 被赋值的状态是当前状态。

     2.当前状态(已存在)能推出哪些状态。 被赋值的状态是被推出来的状态。


    二:

    当设定的k维dp不好递推时,如果再加多一维(这一维是数值,且范围很小)仍不会超时,那么就可以改写成k+1维的dp,这样所有可能的状态都通过dp数组的下标体现出来了,所以要做的就是:递推出存在的状态,然后在这些状态中找出最优结果。


    四维DP:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 100+10;
     9 
    10 int n, m, dp[110][55][2][210];
    11 char s[maxn];
    12 
    13 void init()
    14 {
    15     scanf("%s%d",s+1, &m);
    16     n = strlen(s+1);
    17 
    18     dp[0][0][0][100] = 1;    //起始点平移到100,防止下标溢出
    19     for(int i = 0; i<n; i++)
    20     for(int j = 0; j<=m; j++)  //这样枚举,一个指令最多只能修改一次
    21     for(int dir = 0; dir<2; dir++)
    22     for(int dis = 0; dis<=200; dis++)
    23     {
    24         if(!dp[i][j][dir][dis]) continue;
    25 
    26         dp[i+1][j+(s[i+1]!='F')][dir][dis+(dir?-1:1)] = 1;
    27         dp[i+1][j+(s[i+1]!='T')][!dir][dis] = 1;
    28     }
    29 }
    30 
    31 void solve()
    32 {
    33     int ans = -INF;
    34     for(int j = m; j>=0; j -= 2)  //一个指令可以修改多次
    35     for(int dir = 0; dir<2; dir++)
    36     for(int dis = 0; dis<=200; dis++)
    37     if(dp[n][j][dir][dis])
    38         ans = max(ans, abs(100-dis));     //与100的距离,即为实际距离
    39     cout<<ans<<endl;
    40 }
    41 
    42 int main()
    43 {
    44     init();
    45     solve();
    46 }
    View Code


    三维DP:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 100+10;
     9 
    10 char s[105];
    11 int n,m, dp[110][55][2];
    12 
    13 int main()
    14 {
    15     scanf("%s%d",s+1,&m);
    16     n = strlen(s+1);
    17     for(int i = 0; i<=n; i++)
    18     for(int j = 0; j<=m; j++)
    19     for(int k = 0; k<2; k++)
    20         dp[i][j][k] = -INF;
    21 
    22     dp[0][0][0] = dp[0][0][1] = 0;
    23     for(int i = 0; i<n; i++)
    24     for(int j = 0; j<=m; j++)
    25     for(int k = 0; k<2; k++)
    26     {
    27         dp[i+1][j+(s[i+1]!='F')][k] = max(dp[i+1][j+(s[i+1]!='F')][k],dp[i][j][k]+(k?1:-1));
    28         dp[i+1][j+(s[i+1]!='T')][!k] = max(dp[i+1][j+(s[i+1]!='T')][!k],dp[i][j][k]);
    29     }
    30 
    31     int ans = -INF;
    32     for (int j = m; j>=0; j -= 2)
    33         ans = max(ans, max(dp[n][j][0], dp[n][j][1]));
    34 
    35     printf("%d
    ",ans);
    36 }
    View Code
  • 相关阅读:
    白盒测试
    测试闰年
    黑盒测试
    等价类划分(2)
    等价类的划分
    《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
    《挑战程序设计竞赛》2.5 最短路 AOJ0189 2249 2200 POJ3255 2139 3259 3268(5)
    《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995
    《挑战程序设计竞赛》2.6 数学问题-素数 AOJ0009 POJ3126 3421 3292 3641
    《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538653.html
Copyright © 2011-2022 走看看