HDU 2577 How to Type DP
来源:互联网 发布:qt socket多线程编程 编辑:程序博客网 时间:2024/05/21 09:10
题意:输入一个字符串,要求只能用Caps键和Shift键,问最少的击键次数是多少?
思路:dp[i][2] 表示 输入到第i个字符时Caps亮或者暗的最少步数,dp[i][0] 代表Caps键是暗的,dp[i][1]代表Caps键是亮的。dp方程代码中有解释。
http://acm.hdu.edu.cn/showproblem.php?pid=2577
/********************************************* Problem : HDU 2577 Author : NMfloat InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b) for(int i = a ; i <= b ; i ++)#define rrep(i,a,b) for(int i = b ; i >= a ; i --)#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)#define cls(a,x) memset(a,x,sizeof(a))using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5;const int MAXE = 2e5;typedef long long LL;int T,n,m,k;char a[105];int dp[105][2]; // 2代表Caps键是亮的还是暗的void input() { scanf("%s",a);}void solve() { int lena = strlen(a); if(a[0] >= 'a' && a[0] <= 'z') { dp[0][0] = 1 ; dp[0][1] = 2; } else { dp[0][0] = 2 ; dp[0][1] = 2; } rep(i,1,lena-1) { if(a[i] >= 'a' && a[i] <= 'z') { dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]+2);//输入。 先灭Caps,后输入。 dp[i][1] = min(dp[i-1][0]+2,dp[i-1][1]+2);//先输入,后Caps。 shift加输入。 } else { dp[i][0] = min(dp[i-1][0]+2,dp[i-1][1]+2);//shift加输入。 输入,灭Caps。 dp[i][1] = min(dp[i-1][0]+2,dp[i-1][1]+1);//Caps加输入。 输入。 } } printf("%d\n",min(dp[lena-1][0],dp[lena-1][1]+1));}int main(void) { //freopen("a.in","r",stdin); scanf("%d",&T); while(T--) { //while(~scanf("%d %d",&n,&m)) { input(); solve(); } return 0;}
0 0
- HDU 2577 How To Type DP
- hdu 2577 How to Type(DP)
- hdu 2577 How to Type(DP)
- HDU 2577 How to Type (dp)
- HDU 2577 How to Type(dp)
- 【DP】HDU 2577 How to Type
- HDU-2577 How to Type DP
- hdu 2577 How to Type (dp)
- 【DP|水】HDU-2577 How to Type
- HDU 2577 How to Type (dp)
- hdu 2577 How to Type(dp)
- HDU 2577 How to Type (线性dp)
- hdu 2577 How to Type(DP)
- hdu 2577 How to Type dp
- [HDU 2577 How to Type]DP
- HDU 2577 How to Type(DP)
- HDU 2577 How to Type DP
- hdu 2577 How to Type (dp)
- 【Codeforces Round 330 (Div 2)B】【数值统计 端点思维】Pasha and Phone 电话号码 每块数是x倍数却不能以y开头方案数
- java的反射机制
- LeetCode(226)Invert Binary Tree
- Android 编程下 Touch 事件的分发和消费机制
- 把json对象数组属性相同的进行分组,然后取值
- HDU 2577 How to Type DP
- LeetCode(226)Invert Binary Tree
- Android自定义ViewGroup自动分配子view位置
- 浅谈移动测试
- static代码块,代码块 ,构造方法(一个参数),构造方法(二个参数)
- iOS UIPanGestureRecognizer(拖动手势) 和 点击按钮 实现类似左抽屉的效果
- Android第三方开源NiftyNotification(Android Toast替代品):动画效果生动有趣的通知
- hibernate入门
- ZOJ More Divisors 2562 (反素数)