问题 H: COW

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:26 通过:18

题目描述

奶牛贝茜在她最喜欢的牧场中发现了一块石碑,上面刻有神秘的碑文。

碑文的文字似乎来自一种神秘的古代语言,可看作一个只包含 C,O,W 三种字符的字符串。

尽管贝茜无法解密该文字,但是她很欣赏 C,O,W 按顺序构成她最喜欢的单词 COW。

她想知道 COW 在碑文中一共出现了多少次。

她不介意 C,O,W 之间是否存在其他字符,只要这三个字符按正确的顺序出现即可。

她也不介意多个不同的 COW 是否共享了一些字符。

例如,COW 在 CWOW 中只出现一次,在 CCOW 中出现两次,在 CCOOWW 中出现八次。

给定碑文中的文字,请帮助贝茜计算 COW 出现的次数。

输入格式

第一行包含 N

第二行包含一个长度为 N 的字符串,其中只包含字符 C,O,W。

输出格式

输出给定字符串中 COW 作为子序列(不一定连续)的出现次数。

数据范围

1≤N≤105

输入样例:

6
COOWWW

输出样例:

6

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 C, O, and W. Although Bessie cannot decipher the text, she does appreciate the fact that C, O, and W in sequence forms her favorite word, and she wonders how many times COW appears in the text.

Bessie doesn't mind if there are other characters interspersed within COW, only that the characters appear in the correct order. She also doesn't mind if different occurrences of COW share some letters. For instance, COW appears once in CWOW, twice in CCOW, and eight times in CCOOWW.

Given the text of the inscription, please help Bessie count how many times COW appears.

输入格式

The first line of input consists of a single integer N <= 10^5. The second line contains of a string of N characters, where each character is either a C, O, or W.

输出格式

Output the number of times 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.

输入样例 复制

6
COOWWW

输出样例 复制

6