Problem Description
Pirates have finished developing the typing software. He called Cathy to test his typing software. She is good at thinking. After testing for several days, she finds that if she types a string by some ways, she will type the key at least. But she has a bad habit that if the caps lock is on, she must turn off it, after she finishes typing. Now she wants to know the smallest times of typing the key to finish typing a string.
Input
The first line is an integer t (t<=100), which is the number of test case in the input file. For each test case, there is only one string which consists of lowercase letter and upper case letter. The length of the string is at most 100.
Output
For each test case, you must output the smallest times of typing the key to finish typing this string.
Sample Input
3 Pirates HDUacm HDUACM
Sample Output
8 8 8HintThe 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 8The string "HDUACM", can type this way Caps lock h, d, u, a, c, m, Caps lock, the answer is 8
题意:就是我们日常生活中的一种情况,我们打一串字符(有大写,有小写),字符之间的转化我们可以通过capslock键(刚开始是关闭着的,最后该键一定是关闭的),也可以按shift+字符的方式,对于给定的字符串,问我们最少的按键次数
思路:dp思想的话我们就要考虑当前字符可能的状态,以及该状态可以由那些状态转化而来,在确定一下状态转移方程就可以了
capslock键关着的时候锁定小写,capslock开着的时候锁定大写
dpon[i]表示当前capslock开着的最小次数
dpoff[i]表示当前capslock关闭的最小次数
首先第一步:当前的可能状态
1.当前为小写字符
(1)当前capslock键开着
dpon[i+1]=min(dpon[i]+2,dpoff[i]+2);(2)当前capslock键关闭
dpoff[i+1]=min(dpon[i]+2,dpoff[i]+1);2.当前为大写字符
(1)当前capslock键开着
dpon[i+1]=min(dpon[i]+1,dpoff[i]+2);(2)当前capslock键关闭
dpoff[i+1]=min(dpon[i]+2,dpoff[i]+2);
ac代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; char a[100005]; int dpon[1005]; int dpoff[1005]; int main() { int t; cin>>t; while(t--) { scanf("%s",a); memset(dpon,0,sizeof(dpon)); memset(dpoff,0,sizeof(dpoff)); int len=strlen(a); dpon[0]=1; dpoff[0]=0; for(int i=0;i<len;i++) { if(a[i]>='a'&&a[i]<='z') { dpon[i+1]=min(dpon[i]+2,dpoff[i]+2); dpoff[i+1]=min(dpon[i]+2,dpoff[i]+1); } else { dpon[i+1]=min(dpon[i]+1,dpoff[i]+2); dpoff[i+1]=min(dpon[i]+2,dpoff[i]+2); } } dpon[len]++; cout<<min(dpon[len],dpoff[len])<<endl; } return 0; }