hdu 2577 简单dp

dp[i][state] 记录每个状态当前是大写或小写,扫描一遍

然后每次分情况讨论达到目的的途径

比如按shift 或caps locks

比较简单,也没啥trick

代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cctype>
#define MAX 107

using namespace std;

int t;
char s[MAX];
int dp[MAX][2];

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%s" , s+1 );
        memset ( dp , 0x3f , sizeof ( dp ) );
        dp[0][0] = 0;
        int n = strlen ( s+1 );
        for ( int i = 1 ; i <= n ; i++ )
        {
            if ( isupper( s[i] ) )
            {
                dp[i][1] = min ( dp[i][1] , dp[i-1][0] + 2 );
                dp[i][1] = min ( dp[i][1] , dp[i-1][1] + 1 );
                dp[i][0] = min ( dp[i][0] , dp[i-1][1] + 2 );
                dp[i][0] = min ( dp[i][0] , dp[i-1][0] + 2 );
            }
            if ( islower ( s[i] ) )
            {
                dp[i][1] = min ( dp[i][1] , dp[i-1][0] + 2 );
                dp[i][1] = min ( dp[i][1] , dp[i-1][1] + 2 );
                dp[i][0] = min ( dp[i][0] , dp[i-1][0] + 1 );
                dp[i][0] = min ( dp[i][0] , dp[i-1][1] + 2 );
            }
        }
        printf ( "%d\n" , min ( dp[n][0] , dp[n][1] + 1 ) );
    }
}


你可能感兴趣的:(C++,动态规划)