Title Link: Http://codeforces.com/problemset/problem/180/C
Test instructions
Give you a string that contains only uppercase and lowercase letters, and you can convert lowercase letters to uppercase letters, uppercase to lowercase, and the best steps so that the last string is all uppercase letters to the left and all lowercase letters to the right.
Ideas:
A bit like a tree DP, each letter is treated as a node, and the latter letter is the parent node of the previous letter. Give each node two states:
state One (Dplow[i]) : When the string length is I, the letter i is lowercase to get the minimum number of steps for the string that matches the condition.
state Two (Dpsup[i]) : When the string length is I, the first letter is capitalized, and the minimum number of steps for the string that matches the condition is obtained.
Then the final answer to the last letter of the two States of the value of a small, then the next analysis of the state transfer equation:
If the first letter is lowercase:
for its state one : No conversions are required, the current node itself is lowercase. Since the current state can be transferred from state one and state two of its child nodes, it is better to take the smaller one, DP transfer equation:
Dplow[i] = min (dplow[i-1], dpsup[i-1]);
for its status two : After the conversion is complete, the current node is uppercase, then the current state can only be transferred from the state of its child nodes. So the current value is the value of its child node state two plus 1,DP transfer equation:
Dpsup[i] = dpsup[i-1] + 1;
If the first letter is uppercase:
for its state one : After converting to lowercase, the current state can be transferred from the State and state of its child nodes, so the current value is its child node two state value of the smaller one plus 1,dp transfer equation is:
Dplow[i] = min (dplow[i-1], dpsup[i-1]) + 1;
for its state two : No conversions are required, the current state can only be transferred by the state of its child nodes. So the current value is the value of its child node state two, the DP transfer equation is:
Dpsup[i] = dpsup[i-1];
At this point, the state transition is completed. Implementation, because the parent node is only related to the state of its child nodes, so it is good to use two variables.
Code:
1#include <bits/stdc++.h>2 3 using namespacestd;4 Const intMAXN =100000;5 CharS[MAXN +3];6 7 voidDFS (intKint&tolow,int&tosup) {8 if(k = =0) {Isupper (s[k])? tolow =1: Tosup =1;return ; }9DFS (K-1, Tolow, Tosup); Tolow =min (Tolow, tosup);TenIsupper (S[k])? tolow++: Tosup + +; One } A - intMain () { -Ios_base::sync_with_stdio (0); Cin.tie (0); CIN >>s; the intLen = strlen (s), low =0, sup =0; -DFS (Len-1, Low, SUP); -cout << min (Low, SUP) <<Endl; - return 0; +}
Codeforces 180C. Letter