1430D — String Deletion

Revision en1, by salt_n_ice, 2020-10-12 20:02:35

Problem Here is what I tried:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
using ll = long long;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        vector<ll> len, p;
        ll i=0, j=0;
        while(i<n && j<n)
        {
            while(j<n && s[i]==s[j])
                j++;
            p.push_back(j-i);
            i=j;
        }
        ll ans = 0;
        for(ll i=0; i<p.size(); ++i)
        {
            if(p[i]<2)
            {
                ans++;
                i++;
            }
            else
                ans++;
        }
        cout<<ans<<endl;
    }
}

Can someone briefly explain where am I going wrong?

Tags #greedy, #debug, #brute force, #string

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English salt_n_ice 2020-10-12 20:02:35 881 Initial revision (published)