570C - Replacement - CodeForces Solution


constructive algorithms data structures implementation *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q,counter=0;
    cin>>n>>q;
    string x;
    cin>>x;
    for(int i=0;i<x.length()-1;i++)
    {
        if(x[i]=='.'&&x[i+1]=='.')
            counter++;
    }
    while(q--)
    {
        int a;
        char c;
        cin>>a>>c;
        if(c!='.')
        {
            if(a==1)
            {
                if(x[a-1]=='.')
                {
                    if(x[a]=='.')
                        counter--;
                }
                x[a-1]=c;
            }
            else if(a==n)
            {
                if(x[n-1]=='.')
                {
                    if(x[n-2]=='.')
                        counter--;
                }
                x[n-1]=c;
            }
            else
            {
                if(x[a-1]=='.')
                {
                    if(x[a]=='.')
                        counter--;
                    if(x[a-2]=='.')
                        counter--;
                }
                x[a-1]=c;
            }
        }
        else
        {
            if(a==1)
            {
                if(x[a-1]!='.')
                {
                    if(x[a]=='.')
                        counter++;
                }
                x[a-1]=c;
            }
            else if(a==n)
            {
                if(x[n-1]!='.')
                {
                    if(x[n-2]=='.')
                        counter++;
                }
                x[n-1]=c;
            }
            else
            {
                if(x[a-1]!='.')
                {
                    if(x[a]=='.')
                        counter++;
                    if(x[a-2]=='.')
                        counter++;
                }
                x[a-1]=c;
            }
        }
        cout<< counter<<"\n";
    }

}

 	 	   	 	  	       			 			  		


Comments

Submit
0 Comments
More Questions

746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard