Codeforces Beta Round #96 (Div. 2) (DP)

來源:互聯網
上載者:User
E. Logo Turtletime 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?

<p< p="" style="font-family: verdana, arial, sans-serif; font-size: 14px; line-height: 21px; "><p< p="">

Input

<p< p="">

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.

<p< p=""><p< p="">

Output

<p< p="">

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.

<p< p=""><p< p="">

Sample test(s)

<p< p=""><p< p="">

input
FT1
output
2
input
FFFTFFF2
output
6

<p< p=""><p< p="">

Note

<p< p="">

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.

#include<memory>#include<cstring>#include<iostream>using namespace std;#define N 200#define max(a,b)(a>b?a:b)int main(){    char str[N];    int dp[N][N][2];    int change, len;    cin >> (str+1);    cin >> change;    len = strlen(str+1);    int i, j;    memset(dp,0,sizeof(dp));    for ( i = 0; i <= change; i++ )    {        if ( str[1] == 'F' )        {            dp[1][i][0] = i % 2 - 1;            dp[1][i][1] = 1 - i % 2;        }        else        {            dp[1][i][0] = - (i%2);            dp[1][i][1] = i % 2;        }    }    for ( i = 2; i <= len; i++ )    {        if ( str[i] == 'F' )        {            dp[i][0][0] = dp[i-1][0][0] - 1;            dp[i][0][1] = dp[i-1][0][1] + 1;        }        else        {            dp[i][0][0] = dp[i-1][0][1];            dp[i][0][1] = dp[i-1][0][0];        }        for ( j = 1; j <= change; j++ )        {            if ( str[i] == 'F' )            {                dp[i][j][0] = max ( dp[i-1][j][0] - 1, dp[i-1][j-1][1] );                dp[i][j][1] = max ( dp[i-1][j][1] + 1, dp[i-1][j-1][0] );            }            else            {                dp[i][j][0] = max ( dp[i-1][j][1], dp[i-1][j-1][0] - 1 );                dp[i][j][1] = max ( dp[i-1][j][0], dp[i-1][j-1][1] + 1 );            }        }    }    cout << max(dp[len][change][0], dp[len][change][1]) << endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.