【DP】HDU 2577 How to Type
题意:求最少按键盘多少次
注意当大写开关开着时按shift打出来的是小写
dp[i][j] i==0表示没开大写 i==1 表示开大写;
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #define IN freopen ("in.txt" , "r" , stdin); #define OUT freopen ("out.txt" , "w" , stdout); typedef long long LL; const int MAXN = 111;//点数的最大值 const int MAXM = 20006;//边数的最大值 const int INF = 11521204; const int mod=1000000007; int dp[2][122]; bool daxie(char c) { if(c>='A'&&c<='Z') return true; else return false; } int main() { int t; char s[111]; scanf("%d",&t); while(t--) { scanf("%s",&s); int len=strlen(s); dp[0][0]=0,dp[1][0]=INF; for(int i=1;i<=len;i++) if(daxie(s[i-1]))//大写的情况 { dp[0][i]=min(dp[0][i-1]+2,dp[1][i-1]+2); dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+1); } else//小写 { dp[0][i]=min(dp[0][i-1]+1,dp[1][i-1]+2); dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+2); } printf("%d\n",dp[0][len]); } return 0; }