COW

本文介绍了一种简单的算法,用于统计字符串中作为子序列出现的特定模式“COW”的次数,即使这些字符不连续。文章提供了完整的C++实现代码,并解释了如何处理大数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

COW

时间限制: 1 Sec  内存限制: 64 MB
提交: 41  解决: 18
[提交][状态][讨论版]

题目描述

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
【分析】模拟一下就行了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=100005;
const int M=15005;
char s[N];
int n,m;
ll sc=0,sco=0,scow=0;
int main(){
    scanf("%d%s",&n,s);
    for(int i=0;i<n;i++){
        if(s[i]=='C')sc++;
        if(s[i]=='O')sco+=sc;
        if(s[i]=='W')scow+=sco;
    }
    printf("%lld\n",scow);
    return 0;
}
View Code

 


转载于:https://www.cnblogs.com/jianrenfang/p/5791140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值