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

[Codeforces] 1095A: Repeating Cipher

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

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

 

Problem - 1095A - Codeforces

 

codeforces.com

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		String str = scan.next();
		String result = "";
		int index = 0;
		for(int i=0; i<str.length(); i++) {
			if(index >= str.length())
				break;
			result += str.charAt(index);
			index += (i+1);
		}
		System.out.println(result);
		scan.close();
	}

}

ํ’€์ด

๋ฌธ์ž์—ด s = s1s2 ... sm ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์„๋•Œ, ์•”ํ˜ธ์˜ ๊ทœ์น™์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

s1์€ ํ•œ๋ฒˆ์“ฐ๊ณ , s2๋Š” ๋‘๋ฒˆ์“ฐ๊ณ , s3์€ 3๋ฒˆ์“ฐ๊ณ , ... sm์€ m๋ฒˆ ์“ด๋‹ค.

s = bab๋ผ๋ฉด b aa bbb -> baabbb ๊ฐ€ ๋œ๋‹ค.

 

๋”ฐ๋ผ์„œ ์ฒ˜์Œ ์‚ฌ์šฉ๋œ ๋ฌธ์ž๋งŒ ๋ฝ‘์•„์™”๋‹ค.

s.charAt(0) + s.charAt(1) + s.charAt(3) + s.charAt(6) + s.charAt(10) + ... 

์ด๋Ÿฐ์‹์œผ๋กœ charAt() ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ ๋“ฑ์ฐจ์ˆ˜์—ด์„ ์ด๋ค„์„œ index += (i+1) ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€