题目:Problem D. Clones and Treasures
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 mebibytes
The magical treasury consists of n sequential rooms. Due to construction of treasury its impossible to
go from room with biggest number to room in smallest number, i.e. from first hall one can go only to
second, from second — to third etc.
Each room in treasury may be inhabitated by a clone of ancient king Koschey the Immortal, or, if it is
not inhabitated by a clone, room may contain one gold coin or the invisibility potion which allows become
invisible for time enought to pass only one room with a clone; each not inhabitated room contains exactly
one of those items.
Ivan plans to enter the treasury and collect as much money as possible without being noticed by the
clones of Koschey. He may choose the room where he starts his adventure, then go through some rooms;
at any room he may cast the spell and leave the treasury, but then he will not be able to return anymore.
How much gold coins collects Ivan if he will act optimally?
Input
Input contains one string of length n (1 ≤ n ≤ 106) — map of the treasury. Rooms are listed from lest
to right; character ‘H’ denotes invisibility potion, character ‘M’ — one gold coin, character ‘K’ — clone of
Koschey. String cannot contain characters different than those ones.
Output
Print one integer — maximum amount of gold coins Ivan can collect.
Example
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 char ss[K]; 15 int ans; 16 int main(void) 17 { 18 int len; 19 scanf("%s",ss+1); 20 len=strlen(ss+1); 21 ss[0]=‘K‘; 22 for(int i=1;i<=len;i++) 23 if(ss[i]!=‘K‘) 24 { 25 int ta=0,tb=0; 26 for(i;i<=len;i++) 27 if(ss[i]==‘M‘) ta++; 28 else if(ss[i]==‘H‘) tb++; 29 else if(tb>0) tb--; 30 else break; 31 ans=max(ans,ta); 32 } 33 printf("%d\n",ans); 34 return 0; 35 }
standard input | standard output |
MKHMKMKHMHKKMHKMHHKKKHKMM | 3 |
思路:
贪心扫。