[2455일] A. Stones on the Table (성공)

코드포스 공부기록

목록 보기
22/23

A. Stones on the Table
time limit per test2 seconds
memory limit per test256 megabytes
There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.

Input
The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table.

The next line contains string s, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals "R", if the i-th stone is red, "G", if it's green and "B", if it's blue.

Output
Print a single integer — the answer to the problem.

cpp

#include<iostream>
#include<string>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	string s;
	cin >>n>> s;
	int cnt = 0;
	char fir = ' ';
	for (char i : s)
	{
		if (fir==i)
		{
			cnt++;
		}
		fir = i;
	}
	cout << cnt;
}

c#

using System;
using System.IO;
using System.Text;
using System.Threading;
class Program
{
    static BufferedStream sb = new BufferedStream(Console.OpenStandardInput());
    static StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()), Encoding.Default);
    static StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()), Encoding.Default);
    static int ReadInt()
    {
        int c = sb.ReadByte();
        while (c <= 32) { if (c == -1) return -1; c = sb.ReadByte(); }
        bool neg = false;
        if (c == '-') { neg = true; c = sb.ReadByte(); }
        int val = 0;
        while (c > 32)
        {
            val = val * 10 + (c - '0');
            c = sb.ReadByte();
        }
        return neg ? -val : val;
    }
    static void Solve()
    {
        int n = int.Parse(sr.ReadLine());
        string s = sr.ReadLine();
        char ch = '\0';
        int answer = 0;
        foreach (char i in s)
        {
            if (ch == i) answer++;
            ch = i;
        }
        sw.Write(answer);
        sw.Flush();
    }
    static void Main(string[] args)
    {
        Thread thread = new Thread(Solve, 1024 * 1024 * 32);
        thread.Start();
        thread.Join();
    }
}

python3

import sys
input = sys.stdin.readline
n=int(input().rstrip())
s=input().rstrip()
print( sum(a==b for a, b in zip(s,s [1:] )))

Count the minimum number of stones to take from the table
요 부분 해석을 반대로 해서 문제를 헷갈림.
테이블에서 돌을 빼는 최소 횟수를 세라는 건데

0개의 댓글