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;
    }
    

      

  • 相关阅读:
    Binary Tree Inorder Traversal
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Majority Element
    Excel Sheet Column Number
    Reverse Bits
    Happy Number
    House Robber
    Remove Linked List Elements
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/sola1994/p/4337391.html
Copyright © 2011-2022 走看看