Obviously for each $ o $, consider how much of the left and right, respectively, $ w $, then this contribution is about $ o $ multiplied by the number of $ w $ emergence
W times $ $ $ emergence may be directly obtained according to each period of continuous $ v
Then again sweep from left to right, the left and right about the dynamic maintenance of $ w $, to calculate the contribution to meet $ o $
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long ll; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const int N=2e6+7; int n,tl,tr; char s[N]; ll ans; int main() { scanf("%s",s+1); n=strlen(s+1); int pre=n+1; for(int i=n;i>=1;i--) if(s[i]!='v') { if(pre!=i+1) tr+=pre-i-2; pre=i; } if(pre!=1) tr+=pre-2; pre=0; for(int i=1;i<=n;i++) { if(s[i]=='v') continue; if(pre!=i-1) tl+=i-pre-2,tr-=i-pre-2; ans+=1ll*tl*tr; pre=i; } printf("%lld\n",ans); return 0; }