Atcoder Beginner Content 143 C - Slimes

Calendar Clock iconCalendar Clock icon

atcoder

Table of contents

# Problem

https://atcoder.jp/contests/abc143/tasks/abc143_c

# Explanation

Initialize a counter with 1.
Pick up a character one by one from the head of the string.
Increment the counter as you encounter a different character from the previous one.

# Time complexity

O(N)O(N)

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
#define MAX_N 100001

Int N;
char S[MAX_N];

Int solve() {
  Int count = 1;
  loop(n,1,N) {
    if (S[n] != S[n-1]) count++;
  }
  return count;
}

int main() {
  cin >> N;
  loop(n,0,N) cin >> S[n];
  cout << solve() << endl;
}

Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!

Comments