eolymp
bolt
Try our new interface for solving problems

COW

Bessie the cow has stumbled across an intriguing inscription carved into a large stone in the middle of her favorite grazing field. The text of the inscription appears to be from a cryptic ancient language involving an alphabet with only the three characters \textbf{C}, \textbf{O} and \textbf{W}. Although Bessie cannot decipher the text, she does appreciate the fact that \textbf{C}, \textbf{O} and \textbf{W} in sequence forms her favorite word, and she wonders how many times \textbf{COW} appears in the text. Bessie doesn't mind if there are other characters interspersed within \textbf{COW}, only that the characters appear in the correct order. She also doesn't mind if different occurrences of \textbf{COW} share some letters. For instance, \textbf{COW} appears once in \textbf{CWOW}, twice in \textbf{CCOW}, and eight times in \textbf{CCOOWW}. Given the text of the inscription, please help Bessie count how many times \textbf{COW} appears. \InputFile The first line consists of a single integer $n~(n \le 10^5)$. The second line contains of a string of $n$ characters, where each character is either a \textbf{C}, \textbf{O} or \textbf{W}. \OutputFile Output the number of times \textbf{COW} appears as a subsequence, not necessarily contiguous, of the input string. Note that the answer can be very large, so make sure to use 64 bit integers ("long long" in C++, "long" in Java) to do your calculations.
Time limit 1 second
Memory limit 128 MiB
Input example #1
12
COWCOWCOWCOW
Output example #1
20
Source 2015 USACO February, Bronze