C. Letter
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/180/problem/C
Description
Patrick has just finished writing a message to his sweetheart Stacey when he noticed that the message didn't look fancy. Patrick was nervous while writing the message, so some of the letters there were lowercase and some of them were uppercase.
Patrick believes that a message is fancy if any uppercase letter stands to the left of any lowercase one. In other words, this rule describes the strings where first go zero or more uppercase letters, and then — zero or more lowercase letters.
To make the message fancy, Patrick can erase some letter and add the same letter in the same place in the opposite case (that is, he can replace an uppercase letter with the lowercase one and vice versa). Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions.
Input
The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105.
Output
Print a single number — the least number of actions needed to make the message fancy.
Sample Input
PRuvetSTAaYA
Sample Output
5
HINT
题意
给你一个字符串,每次操作你可以将一个大写字母变成小写字母或者小写字母变成大写字母
然后问你最少多少次操作,可以使得这个字符串变成,左边全是大写,右边全是小写字母的形式
题解:
直接暴力枚举有多少个大写字母就好了
代码
#include<iostream> #include<stdio.h> #include<cstring> using namespace std; #define maxn 100005 char s[maxn]; int low[maxn],high[maxn]; int main() { scanf("%s",s+1); int len = strlen(s+1); for(int i=1;i<=len;i++) { low[i]=low[i-1]; high[i]=high[i-1]; if(s[i]<='z'&&s[i]>='a') low[i]++; else high[i]++; } int ans = 9999999; for(int i=0;i<=len;i++) { ans = min(ans,i-high[i]+(high[len]-high[i])); } printf("%d ",ans); }