codeforce1178B (DP)

题意描述

Recall that string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly zero or all) characters. For example, for the string a=“wowwo”, the following strings are subsequences: “wowwo”, “wowo”, “oo”, “wow”, “”, and others, but the following are not subsequences: “owoo”, “owwwo”, “ooo”.

The wow factor of a string is the number of its subsequences equal to the word “wow”. Bob wants to write a string that has a large wow factor. However, the “w” key on his keyboard is broken, so he types two "v"s instead.

Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string “ww”. Bob would type it as “vvvv”, but this string actually contains three occurrences of “w”:

" vvvv"
“v vvv”
“vv vv”
For example, the wow factor of the word “vvvovvv” equals to four because there are four wows:

" vvv o vvv"
" vvv ov vv"
“v vv o vvv”
“v vv ov vv”
Note that the subsequence " vv v o vvv" does not count towards the wow factor, as the "v"s have to be consecutive.

For a given string s, compute and output its wow factor. Note that it is not guaranteed that it is possible to get s from another string replacing “w” with “vv”. For example, s can be equal to “vov”.

Input
The input contains a single non-empty string s, consisting only of characters “v” and “o”. The length of s is at most 106.

Output
Output a single integer, the wow factor of s.

思路

这道题只需要动态维护每个o左右两边w的数量,然后相乘即可,所以我们左右扫一遍,然后求出左右两边w的数量即可。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#pragma GCC optimize(2)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e6+10;
const int M=1e6;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
int l[N],r[N];
char s[N];
int main()
{
    IOS;
    cin>>s+1;
    int n=strlen(s+1);
    LL ans=0;
    for(int i=1;i<=n;i++){
        l[i]=l[i-1];
        if(i>1 && s[i]=='v' && s[i-1] =='v') l[i]++;
    }
    for(int i=n;i>=1;i--){
        r[i]=r[i+1];
        if(i<n && s[i]=='v' && s[i+1]=='v') r[i]++;
    }
    for(int i=1;i<=n;i++){
        if(s[i]=='o') ans+=(LL)l[i-1]*r[i+1];
    }
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值