zoukankan      html  css  js  c++  java
  • 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
    ",min(dp[len-1][0]+1,dp[len-1][1]));   
      }
      return 0;
    }
    

      

  • 相关阅读:
    C# 中的var关键字
    sql中去掉换行符和回车符
    Linq之旅:Linq入门详解(Linq to Objects)
    c# for 和 foreach
    c# Dictionary
    ASP.NET Web.config学习
    c# 装箱与拆箱的概念
    c# List集合学习
    Index was out of range
    C# double保留四位小数
  • 原文地址:https://www.cnblogs.com/sola1994/p/4337391.html
Copyright © 2011-2022 走看看