Hdu 2577 How to Type (DP)(对题意有些疑问。。)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577

题意:输入一行只有大写字母和小写字母组成的字符串,串长不超过100.问最少按多少个按键。

原思路:dp

AC代码:

#include<stdio.h>
#include<string.h>
#define maxn 100+5
#define LL __int64
#define INF 9999999
#define Shift 1
#define Caps 1
int dp[maxn][2];//dp[i][0/1]表示在第i位结束的时候是(是否按Caps)状态下的最小num;
bool up(char a){
	if(a >='A' && a<='Z')
		return true;
	return false;
}
bool low(char a){
	if(a>='a' && a<='z')
		return true;
	return false;
}
int min(int a,int b){
	int ans = a;
	if(ans > b)ans = b;
	return ans;
}
int main(){
	int n;
	scanf("%d",&n);
	int ans = 0,len;
	char s[105];
	while(n--){
		scanf("%s",s);
		len = strlen(s);
		if(up(s[0]))dp[0][Caps] = 2,dp[0][0] = 2;
		if(low(s[0]))dp[0][Caps] = 2,dp[0][0] = 1;
		for (int i = 1; i < len; i++)
		{
			if(up(s[i])){//转移方程,自己数就好了
				dp[i][Caps] = min(dp[i-1][Caps] + 1,dp[i-1][0] + 2);
				dp[i][0]	= min(dp[i-1][Caps] + 2,dp[i-1][0] + 2);
			}else{			 								  
				dp[i][Caps] = min(dp[i-1][Caps] + 2,dp[i-1][0] + 2);
				dp[i][0]	= min(dp[i-1][Caps] + 2,dp[i-1][0] + 1);
			}
		}
		ans = min(dp[len-1][0],dp[len-1][Caps]+1);
		printf("%d\n",ans);
	}	
	return 0;
}

本来最先写出来的是上面的代码,但是自己试了组数据“PP”,发现输出4. 很诧异(难道不是“Shift,p,p”3个键吗??)。

之后再看题目给的用例:

HDUacm
HDUACM

题目给的hint:

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

可是 :

“HDUacm”--Shift,h,d,u,(松开Shift),a,c,m 就是7键。

“HDUACM”--Shift,h,d,u,a,c,m 也是7个键。

对于题意不是很明白了,但还是又写了个考虑Shift的程序(数数好费劲。。)

代码:

#include<stdio.h>
#include<string.h>
#define maxn 100+5
#define LL __int64
#define INF 9999999
#define Shift 1
#define Caps 1
int dp[maxn][2][2];//dp[i][0/1][0/1]表示在第i位结束的时候是(是否按Caps)(是否按shift)状态下的最小num;
bool up(char a){
	if(a >='A' && a<='Z')
		return true;
	return false;
}
bool low(char a){
	if(a>='a' && a<='z')
		return true;
	return false;
}
int min(int a,int b,int c,int d){
	int ans = a;
	if(ans > b)ans = b;
	if(ans > c)ans = c;
	if(ans > d)ans = d;
	return ans;
}
int main(){
	int n;
	scanf("%d",&n);
	int ans = 0,len;
	char s[105];
	while(n--){
		scanf("%s",s);
		len = strlen(s);
		if(up(s[0]))dp[0][Caps][0] = 2,dp[0][0][0] = 2,dp[0][Caps][Shift] = 3,dp[0][0][Shift] = 2;
		if(low(s[0]))dp[0][Caps][0] = 2,dp[0][0][0] = 1,dp[0][Caps][Shift] = 3,dp[0][0][Shift] = 2;
		for (int i = 1; i < len; i++)
		{
			if(up(s[i])){//转移方程,自己数就好了
				dp[i][Caps][0]		= min(dp[i-1][Caps][0] + 1,dp[i-1][0][0] + 2,dp[i-1][Caps][Shift] + 1,dp[i-1][0][Shift] + 2);
				dp[i][Caps][Shift]  = min(dp[i-1][Caps][0] + 2,dp[i-1][0][0] + 3,dp[i-1][Caps][Shift] + 2,dp[i-1][0][Shift] + 2);
				dp[i][0][0]			= min(dp[i-1][Caps][0] + 2,dp[i-1][0][0] + 2,dp[i-1][Caps][Shift] + 2,dp[i-1][0][Shift] + 2);
				dp[i][0][Shift]		= min(dp[i-1][Caps][0] + 3,dp[i-1][0][0] + 2,dp[i-1][Caps][Shift] + 2,dp[i-1][0][Shift] + 1);
			}else{
				dp[i][Caps][0]		= min(dp[i-1][Caps][0] + 2,dp[i-1][0][0] + 2,dp[i-1][Caps][Shift] + 1,dp[i-1][0][Shift] + 2);
				dp[i][Caps][Shift]  = min(dp[i-1][Caps][0] + 2,dp[i-1][0][0] + 3,dp[i-1][Caps][Shift] + 1,dp[i-1][0][Shift] + 2);
				dp[i][0][0]			= min(dp[i-1][Caps][0] + 2,dp[i-1][0][0] + 1,dp[i-1][Caps][Shift] + 2,dp[i-1][0][Shift] + 1);
				dp[i][0][Shift]		= min(dp[i-1][Caps][0] + 3,dp[i-1][0][0] + 2,dp[i-1][Caps][Shift] + 2,dp[i-1][0][Shift] + 2);
			}
		}
		ans = min(dp[len-1][0][Shift],dp[len-1][0][0],dp[len-1][Caps][0]+1,dp[len-1][Caps][1]+1);
		printf("%d\n",ans);
	}	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值