[USACO2015February,Bronze] Problem2.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 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.

INPUT FORMAT: (file cow.in)

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 FORMAT: (file cow.out)

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.

SAMPLE INPUT:
6
COOWWW
SAMPLE OUTPUT:
6

[Problem credits: Ben Cousins and Brian Dean, 2015]

数据范围是 105 ,三层for循环肯定超时.
让我们考虑一下线性的算法,显然对我们来说重要的序列只有”C”,”CO”和”COW”,通过简单的分析我们可以发现:

  1. 如果当前的字符是’W’,那么”COW”出现的次数应该加上前面”CO”出现的次数;
  2. 如果当前的字符是’O’,那么”CO”出现的次数应该加上前面”C”出现的次数;
  3. 如果当前的字符是’C’,那么”C”出现的次数应该加1.

执行完这个过程,我们就能很容易得出”COW”出现的次数.

代码如下:

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
int n;
LL C = 0, O = 0, W = 0;
string S;
int main()
{
  freopen("cow.in", "r", stdin);
  freopen("cow.out", "w", stdout);
  cin >> n >> S;
  for(int i = 0; i < n; i++)
  {
    if(S[i] == 'C')
      C++;
    else if(S[i] == 'O')
      O += C;
    else if(S[i] == 'W')
      W += O;
  }
  cout << W << endl;
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值