๋ฐ์ํ
https://codeforces.com/problemset/problem/1285/A
์ฝ๋
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
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Codeforces] 1077A: Frog Jumping (0) | 2020.03.08 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ์์ฐ (0) | 2020.03.08 |
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ์์ ์ฐพ๊ธฐ (0) | 2020.03.07 |
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ (0) | 2020.03.07 |
[๋ฐฑ์ค] 5052๋ฒ: ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2020.03.07 |
๋๊ธ