Need help with a problem from the latest contest

Revision en1, by ChinaNumberOne, 2019-07-21 15:56:11

Hey!

so In the contest yesterday, I tried to solve problem B, but I think I misunderstanded it, and even after I read the tutorial I still did not get it!!

I understood that I have to print the number of ways of getting "wow" from string s. My idea was to calculate number of 'v' before(prev) and after(nxt) every 'o', then the answer would be the sum of ( ( prev — 1 ) * ( nxt — 1 ) ).

for example : "vvvovvv" -> nxt = prev = 3 -> ans = 2 * 2 = 4

So can anyone help me to know where I did the mistake ??

Here is my code :

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main () 
{
    
    string s ;
    cin >> s ;
    
    int ctr = 0 ;
    for ( int i = 0 ; i < s.length() ; i ++ )
        if ( s[i] == 'v' )
            ctr ++ ;
    
    ll ans = 0 ;
    ll prev = 0 ;
    ll nxt = ctr ;
    
    for ( int i = 0 ; i < s.length() ; i ++ )
    {
        if ( s[i] == 'o' && prev && nxt )
            ans += ( prev - 1 ) * ( nxt - 1 ) ;
        
        else
        {
            prev ++ ;
            nxt -- ;
        }
    }
    
    cout << ans ;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ChinaNumberOne 2019-07-21 15:56:11 1257 Initial revision (published)