前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforce1178B (DP)

codeforce1178B (DP)

作者头像
dejavu1zz
发布2020-10-23 15:17:26
5190
发布2020-10-23 15:17:26
举报

题意描述

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档