http://acm.hdu.edu.cn/diy/diy_previewproblem.php?cid=19572&pid=1016
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
How to Type
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
[Hint]
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
[/Hint]
Author
Dellenge
Source
HDU 2009-5 Programming Contest
解析:开关等问题,开始的时候我用分类讨论法,应该是想的不够周到吧,老频频出错
后来改用状态转移模拟法,类比开灯问题。,才得以解决
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn=100+10;
char ch[maxn];
int dp[maxn][2];//dp[j][1]关灯,dp[j][0]开灯
int min(int a,int b)
{
return a<b? a:b;
}
int main()
{
int i,T;
int len,m;
scanf("%d",&T);
while(T--)
{
cin>>ch+1;//表示指针向后移动一位,目的在于ch[0]为空,便于初始化
len=strlen(ch+1);
printf("%c",ch[len]);
memset(dp,0,sizeof(dp));
dp[0][0]=1;//设初始化状态是关灯,因此若要开灯则必须按键;
for(i=1;i<=len;i++)
{
if(ch[i]<='z'&&ch[i]>='a')
{ dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+1);
//开灯则按Caps lock+字母回到当前关灯则直接按字母
dp[i][0]=min(dp[i-1][0]+2,dp[i-1][1]+2);//当处于当前处于开灯时;开灯则按shift+字母,回到当前。
//关灯则按字母+Caps lock
}
if(ch[i]<='Z'&&ch[i]>='A')
{dp[i][1]=min(dp[i-1][0]+2,dp[i-1][1]+2);//开灯则按字母+Caps lock,关灯则shift+字母
dp[i][0]=min(dp[i-1][0]+1,dp[i-1][1]+2);//开灯则按字母,关灯则按Caps lock+字母
}
}
dp[len][0]++;
m=min(dp[len][0],dp[len][1]);
printf("%d\n",m);
}
return 0;
}