标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5452 Accepted Submission(s): 2436
16464331 | 2016-03-07 10:38:54 | Accepted | 2577 | 0MS | 1564K | 1191 B | G++ | czy |
题解:
转自:http://blog.sina.com.cn/s/blog_892d87ff0100u1n5.html
解题报告:题意:输入以字符串,求最少需要敲多少下键盘才能完成输入!开两个数组open[N],close[N],分别记录灯亮时输入第i个字符所敲键盘的次数,灯灭时输入第i个字符时所敲键盘的次数!
一开始没想到,可以在大写锁定下,按Shift输入小写,唉,dp的意识还是不强,有待继续训练
转移方程:
1 if(judge(s[i]) == 1){ 2 open[i] = min(open[i - 1] + 1,close[i - 1] + 2); 3 close[i] = min(open[i - 1] + 2,close[i - 1] + 2); 4 } 5 else{ 6 open[i] = min(open[i - 1] + 2,close[i - 1] + 2); 7 close[i] = min(open[i - 1] + 2,close[i - 1] + 1); 8 }
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 #include <cctype> 7 #include <vector> 8 #include <cmath> 9 #include <map> 10 #include <queue> 11 12 #define ll long long 13 #define N 10005 14 #define eps 1e-8 15 16 using namespace std; 17 18 int T; 19 int ans; 20 char s[105]; 21 int l; 22 int open[105]; 23 int close[105]; 24 25 int judge(char c) 26 { 27 if(c >= ‘A‘ && c <= ‘Z‘) return 1; 28 return 0; 29 } 30 31 void solve() 32 { 33 int i; 34 i = 0; 35 open[i] = 1; 36 close[i] = 0; 37 for(i = 1;i <= l;i++){ 38 if(judge(s[i]) == 1){ 39 open[i] = min(open[i - 1] + 1,close[i - 1] + 2); 40 close[i] = min(open[i - 1] + 2,close[i - 1] + 2); 41 } 42 else{ 43 open[i] = min(open[i - 1] + 2,close[i - 1] + 2); 44 close[i] = min(open[i - 1] + 2,close[i - 1] + 1); 45 } 46 } 47 } 48 49 int main() 50 { 51 //freopen("in.txt","r",stdin); 52 scanf("%d",&T); 53 for(int ccnt=1;ccnt<=T;ccnt++){ 54 //while(scanf("%I64d%I64d",&a,&b)!=EOF){ 55 scanf("%s",s + 1); 56 l = strlen(s + 1); 57 solve(); 58 ans = min(open[l] + 1,close[l]); 59 printf("%d\n",ans); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/njczy2010/p/5249734.html