1165C - Good String - CodeForces Solution


greedy *1300

Please click on ads to support us..

Python Code:

import sys,os
from collections import defaultdict,Counter
from math import ceil,gcd,sqrt
from bisect import bisect_left

f=False
INT_MAX=sys.maxsize
if os.getcwd()=="/Users/mehtaparityatinbhai/Desktop/test_codes":
	sys.stdin = open('inp.txt', 'r')
	sys.stdout = open('out.txt', 'w')
	f=True

def lcm(a,b):
	return (a*b)//gcd(a,b)

def readint():
	return int(input())

def readlst():
	return list(map(int, input().split()))


def leftrotate(a,l,r):
	return a[l+1:r]+a[:l+1]+a[r:]


def is_prime(n):
    if n < 5 or n & 1 == 0 or n % 3 == 0:
        return 2 <= n <= 3
    s = ((n - 1) & (1 - n)).bit_length() - 1
    d = n >> s
    for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
        p = pow(a, d, n)
        if p == 1 or p == n - 1 or a % n == 0:
            continue
        for _ in range(s):
            p = (p * p) % n
            if p == n - 1:
                break
        else:
            return False
    return True





def sol(cnt):
	n=readint()
	s=input()
	res=''
	for i in range(n):
		if len(res)&1==0:
			res+=s[i]
		else:
			if res[-1]==s[i]:
				continue
			else:
				res=res+s[i]
	if len(res)&1:
		res=res[:-1]
	print(len(s)-len(res))
	print(res)
	return

def main():
	tc=1
		cnt=0
	for _ in range(tc):
		cnt+=1
		sol(cnt)
main()


C++ Code:

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

#define fast ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL)

#define endl "\n"

#define pb push_back

#define popb pop_back

#define mod 1000000007

# define f first

#define s second

int main()

{

    ll t=1;

    fast;

    //cin>>t;

    while(t--)

    {

        ll n;

        cin>>n;

        string s;

        cin>>s;

        stack<char>ss;

        ss.push(s[0]);

        for (ll i=1;i<n;i++)

        {

            if(ss.top()!=s[i])

                ss.push(s[i]);

            else

            {

                if (ss.size()%2==0)

                    ss.push(s[i]);

            }

        }

        while(ss.size()%2)

            ss.pop();

        string ans="";

        while(!ss.empty())

        {

            ans+=ss.top();

            ss.pop();

        }

        reverse(ans.begin(),ans.end());

        cout<<n-ans.size()<<endl;

        cout<<ans<<endl;

    }



}


Comments

Submit
0 Comments
More Questions

988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out