用open[],close[]记录每次决策后的最小敲击次数
1 #include<stdio.h> 2 #include<string.h> 3 int open[105]; 4 int close[105]; 5 int main() 6 { 7 int t; 8 scanf("%d",&t); 9 while(t--) 10 { 11 char str[105]; 12 scanf("%s",str); 13 memset(open,0,sizeof(open)); 14 open[0]=1; 15 memset(close,0,sizeof(close)); 16 int len=strlen(str); 17 for(int i=0;i<len;i++) 18 { 19 if(str[i]>='a'&&str[i]<='z') 20 { 21 open[i+1]=open[i]+2<close[i]+2?open[i]+2:close[i]+2; 22 close[i+1]=open[i]+2<close[i]+1?open[i]+2:close[i]+1; 23 } 24 else 25 { 26 open[i+1]=open[i]+1<close[i]+2?open[i]+1:close[i]+2; 27 close[i+1]=open[i]+2<close[i]+2?open[i]+2:close[i]+2; 28 } 29 } 30 open[len]++; 31 printf("%d\n",close[len]<open[len]?close[len]:open[len]); 32 } 33 return 0; 34 }