您的位置:首页 > 其它

hdu 2577 How to Type(DP)

2015-03-11 19:42 351 查看
题意:

给一个字符串(有大写有小写),问最少需要按多少次【键盘上的键】才能写出来。

开始前大写灯是关着的。结束后也必须保证大写灯是关的。

例:

Pirates  HDUacm  HDUACM

8 8 8

The string “Pirates”, can type this way, Shift, p, i, r, a, t, e, s, the answer is 8.

The string “HDUacm”, can type this way, Caps lock, h, d, u, Caps lock, a, c, m, the answer is 8

The string "HDUACM", can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8

思路:

每打完一个字母大写灯都可能有两种状态。层次结构还是比较易看出来的。

dp[i][0]:打完第i个字母大写灯是关闭状态所花最少次数。

dp[i][1]:打完第i个字母大写灯是开启状态所花最少次数。

代码:

int dp_closed[105];
int dp_open[105];

int main(){

int T;
char str[105];

cin>>T;
while(T--){
scanf("%s",str);
int L=strlen(str);

dp_open[0]=1;
dp_closed[0]=0;
rep(i,1,L-1){
if(str[i-1]>='a' && str[i-1]<='z'){
dp_closed[i]=min( dp_closed[i-1]+1,dp_open[i-1]+2 );
dp_open[i]=min( dp_closed[i-1]+2,dp_open[i-1]+2 );
}else{
dp_closed[i]=min( dp_closed[i-1]+2,dp_open[i-1]+2 );
dp_open[i]=min( dp_closed[i-1]+2,dp_open[i-1]+1 );
}
}
if(str[L-1]>='a' && str[L-1]<='z'){
dp_closed[L]=min( dp_open[L-1]+2,dp_closed[L-1]+1 );
}else{
dp_closed[L]=min( dp_open[L-1]+2,dp_closed[L-1]+2 );
}
printf("%d\n",dp_closed[L]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: