How to Type
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 36 Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 8 <div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div> 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 </div>
这个题目虽然你的动态转移方程可以写出来但是一不小心还是会WA
动态方程如下:
如果输入的是大写字母
{
dp[0][i]=min(dp[0][i-1]+2,dp[1][i-1]+2);
dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+1);
}
如果输入的是小写字母
{
dp[0][i]=min(dp[0][i-1]+1,dp[1][i-1]+2);
dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+2);
}
主要是赋初值当第一个字母是大写时首先打开大写算一次 按下键算一次 所以dp[0][0]=2;否则dp[0][0]=1;
无论输入的是大写还是小写dp[1][0]=2;
#include <string.h>
#include <iostream>
using namespace std;
int dp[2][105];
int main()
{
dp[0][0]=0;
dp[1][0]=1;
int t,i,len;
string s;
cin>>t;
while (t--)
{
memset(dp,0,sizeof(dp));
cin>>s;
len=s.size();
if(s[0]>='A'&&s[0]<='Z')
dp[0][0]=2;
else
dp[0][0]=1;
dp[1][0]=2;
for(i=1;i<=len-1;i++)
{
if (s[i]>='A'&&s[i]<='Z')//输入大写
{
dp[1][i]=min(dp[1][i-1]+1,dp[0][i-1]+2);
dp[0][i]=min(dp[1][i-1]+2,dp[0][i-1]+2);
}
else //输入小写
{
dp[1][i]=min(dp[1][i-1]+2,dp[0][i-1]+2);
dp[0][i]=min(dp[0][i-1]+1,dp[1][i-1]+2);
}
}
cout<<min(dp[0][len-1],dp[1][len-1]+1)<<endl;
}
return 0;
}