友情题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2577
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
char str[N];
int close[N],open[N];
int MIN(int a ,int b)
{
return a=a<b?a:b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(close,0,sizeof(close));
memset(open,0,sizeof(open));
//open和close数组分别表示打一个字母时对应CapsLock灯的状态
open[0]=1;
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
//当前为open状态对应上一个字母的open和close两个状态
//1.上次为open,当前也为open,直接敲 +1
//2.上次为close,当前为open,敲CapsLock->字母 +2
open[i+1]=MIN(open[i]+1,close[i]+2);
//1.上次为open,当前为close,敲字母 -> CapsLock +2
//2.上次为close,当前为close, 敲Shift+字母 +2
close[i+1]=MIN(open[i]+2,close[i]+2);
}
else
{
//同上!!!
open[i+1]=MIN(open[i]+2,close[i]+2);
close[i+1]=MIN(open[i]+2,close[i]+1);
}
}
open[len]++;
int ans=MIN(open[len],close[len]);
printf("%d\n",ans);
}
return 0;
}
//版权所有,转载请注明出处,否则追究其法律责任!