๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Codeforces] 1285A: Mezo Playing Zoma

by ์ฃผ๋ฐœ2 2020. 3. 8.
๋ฐ˜์‘ํ˜•

https://codeforces.com/problemset/problem/1285/A

 

Problem - 1285A - Codeforces

 

codeforces.com

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(bf.readLine());
		String str = bf.readLine();
		int left = 0;
		int right = 0;
		for(int i=0; i<str.length(); i++) {
			left += (str.charAt(i) == 'L') ? -1 : 0;
			right += (str.charAt(i) == 'R') ? 1 : 0;
		}
		System.out.println((right - left) + 1);
		bf.close();
	}

}

ํ’€์ด

L = x-1 , R = x+1 ๋งŒํผ ์›€์ง์ผ ์ˆ˜ ์žˆ๊ณ ,

๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„๋•Œ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋‹ค๋ฅธ ์ขŒํ‘œ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ.

 

์ฃผ์–ด์ง„ L, R ๋งŒํผ ์ขŒ, ์šฐ๋กœ ์ด๋™์‹œํ‚ค๋ฉด์„œ ๋‘๊ฐœ์˜ ๋บ€ ๊ฐ’ + 1 ์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋ฌธ์ž์—ด = LRLR ์ผ ๋•Œ,

L์ด 2๊ฐœ, R์ด 2๊ฐœ์ด๋ฏ€๋กœ ๊ฐ๊ฐ ์ขŒ, ์šฐ๋กœ ๊ฐˆ์ˆ˜์žˆ๋Š” ์ตœ๋Œ€ ์œ„์น˜๋Š” -2(L 2๋ฒˆ) , +2(R 2๋ฒˆ)์ด๋‹ค.

๋”ฐ๋ผ์„œ -2 ~ +2 ๊นŒ์ง€ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.(-2, -1, 0, 1, 2)

-> 2 - (-2) + 1 = 5

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€