hdu 2577 How to Type(dp)

题意: 输入一行字符串 只包含大小写字母

         可以使用shift 和 caps 键 切换大小写

         问最少按几次键

 

思路:

if(str[i]>='A'&&str[i]<='Z')
{
dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2);//on
dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2);//off
}
else
{
dp[i][0]=min(dp[i-1][0]+2,dp[i-1][1]+2);
dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+1);
}

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[200][5];
char str[200];
int main()
{
  int t;
  int len;
  int i,j,k;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%s",str);
    len=strlen(str);
    if(str[0]>='A'&&str[0]<='Z')
      {
        dp[0][0]=2;
        dp[0][1]=2;
      }
      else
      {
        dp[0][0]=2;
        dp[0][1]=1;
      }
    for(i=1;i<len;i++)
    {
      if(str[i]>='A'&&str[i]<='Z')
      {
        dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2);//on
        dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2);//off
      }
      else
      {
        dp[i][0]=min(dp[i-1][0]+2,dp[i-1][1]+2);
        dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+1);
      }
    }
    printf("%d\n",min(dp[len-1][0]+1,dp[len-1][1]));   
  }
  return 0;
}

  

posted @ 2015-03-14 13:12  sola94  阅读(97)  评论(0编辑  收藏  举报