HDU 2577 How to Type(dp)

来源:互联网 发布:用友网络新浪 编辑:程序博客网 时间:2024/05/16 14:19

当在第i个字符时,会有两种情况,一种是灯关着,另一种是灯开着,所以要分别进行dp,看最后哪种少。

////  main.cpp//  Richard////  Created by 邵金杰 on 16/9/13.//  Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=100+10;int dp[maxn][2];char s[maxn];int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%s",s+1);        dp[0][0]=0;        dp[0][1]=1;        for(int i=1;s[i];i++)        {            if(s[i]>='a')            {                dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2);                dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2);            }            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);            }        }        cout<<min(dp[strlen(s+1)][0],dp[strlen(s+1)][1]+1)<<endl;    }    return 0;}


0 0
原创粉丝点击