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

[๋ฐฑ์ค€] 2966๋ฒˆ: ์ฐ๊ธฐ(์™„์ „ํƒ์ƒ‰, brute force)

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

https://www.acmicpc.net/problem/2966

 

2966๋ฒˆ: ์ฐ๊ธฐ

๋ฌธ์ œ ์ƒ๊ทผ์ด, ์ฐฝ์˜์ด, ํ˜„์ง„์ด๋Š” ์—ญ์‚ฌ์™€ ์ „ํ†ต์„ ์ž๋ž‘ํ•˜๋Š” Sogang ACM-ICPC Team์— ๊ฐ€์ž…ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ€์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ชจ๋“  ์ง€์›์ž๋Š” C์–ธ์–ด ํ•„๊ธฐ์‹œํ—˜์„ ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค. ์ด๋“ค์€ C์–ธ์–ด๋ฅผ ํ•  ์ค„ ๋ชจ๋ฅธ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•„๊ธฐ์‹œํ—˜์„ ๋ชจ๋‘ ์ฐ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ƒ๊ทผ์ด๋Š” A, B, C, A, B, C, A, B, C, A, B, C, ...์™€ ๊ฐ™์ด ์ฐ์–ด์•ผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.  ํ•˜์ง€๋งŒ, ์ฐฝ์˜์ด๋Š” B, A, B, C, B, A, B, C, B, A, B

www.acmicpc.net

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		/** 0 **/
		char[] A = {'A', 'B', 'C'};
		char[] B = {'B', 'A', 'B', 'C'};
		char[] G = {'C', 'C', 'A', 'A', 'B', 'B'};
		boolean flag = true;
		String[] name = {"Adrian", "Bruno", "Goran"};
		int[] rightCount = new int[3];
		String answer = scan.next();
		
		/** 1 **/
		for(int i=0; i<answer.length(); i++) {
			for(int j=0; j<A.length; j++) {
				if(i+j >= answer.length()) {
					flag = false;
					break;
				}
				
				if(answer.charAt(i+j) == A[j])
					rightCount[0] ++;
			}
			if(!flag)
				break;
			i += A.length-1;
		}
		
		/** 2 **/
		flag = true;
		for(int i=0; i<answer.length(); i++) {
			for(int j=0; j<B.length; j++) {
				if(i+j >= answer.length()) {
					flag = false;
					break;
				}
				
				if(answer.charAt(i+j) == B[j])
					rightCount[1] ++;
			}
			if(!flag)
				break;
			i += B.length-1;
		}
		
		/** 3 **/
		flag = true;
		for(int i=0; i<answer.length(); i++) {
			for(int j=0; j<G.length; j++) {
				if(i+j >= answer.length()) {
					flag = false;
					break;
				}
				
				if(answer.charAt(i+j) == G[j])
					rightCount[2] ++;
			}
			if(!flag)
				break;
			i += G.length-1;
		}
		
		/** 4 **/
		int max = 0;
		for(int i=0; i<rightCount.length; i++)
			if(rightCount[i] > max)
				max = rightCount[i];
		System.out.println(max);
		
		/** 5 **/
		for(int i=0; i<name.length; i++) {
			if(max == rightCount[i])
				System.out.println(name[i]);
		}
		
		scan.close();
	}

}

 

๋ฌธ์ œ ์ดํ•ด

๊ตฌํ˜„์ด ๊ฝค ๊นŒ๋‹ค๋กœ์› ๋‹ค... ์ƒ๊ฐํ•  ๊ฒƒ์ด ์ข€ ์žˆ๊ธฐ๋„ํ•ด๊ณ , ๋ณต์žกํ•˜๊ฒŒ ํ’€์–ด์„œ ์ฝ”๋“œ๋„ ๊ธธ์–ด์กŒ๋‹ค.

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์ƒ๊ฐํ•œ ํ’€์ด๋Š” , 

์ƒ๊ทผ, ์ฐฝ์˜, ํ˜„์ง„์ด๊ฐ€ ๊ฐ๊ฐ ๋ช‡๋ฌธ์ œ๋ฅผ ๋งžํ˜”๋Š”์ง€ ์ „๋ถ€ count๋ฅผ ํ•˜๊ณ ,

๊ฐ€์žฅ ๋งŽ์ด ๋งžํžŒ ํ•™์ƒ์„ ์ถœ๋ ฅํ•˜๋Š”, ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€๋ ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

 

ํ’€์ด

์ƒ๊ทผ์ด๋Š” A, B, C ์ˆœ์œผ๋กœ ์ฐ๊ณ 

์ฐฝ์˜์ด๋Š” B, A, B, C ์ˆœ์œผ๋กœ ์ฐ๊ณ 

ํ˜„์ง„์ด๋Š” C, C, A, A, B, B ์ˆœ์œผ๋กœ ์ฐ๋Š”๋‹ค.

๋”ฐ๋ผ์„œ ์œ„ ์„ธ๋ช…์ด ์ฐ๋Š” ๋ฐฉ์‹์„ ๋ฐฐ์—ด๋กœ ์ €์žฅํ•œ๋‹ค.

 

/** 0 **/

char[] A, B, G

 - ๊ฐ ํ•™์ƒ์ด ์ฐ๋Š” ๊ทœ์น™์„ ์ €์žฅํ•˜๊ณ ,

 

boolean flag

for๋ฌธ์—์„œ ๋ฒ”์œ„๊ฐ€ ๋ฒ—์–ด๋‚ ๊ฒฝ์šฐ break๋ฅผ ๊ฑธ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ์„ ์–ธํ–ˆ๋‹ค.

 - ์˜ˆ์ œ ์ž…๋ ฅ 1์„ ์˜ˆ๋กœ๋“ค๋ฉด, ์ •๋‹ต์€ BAACC๋กœ ๊ธธ์ด๊ฐ€ 5์ง€๋งŒ, ํ˜„์ง„์ด๊ฐ€ ์ฐ๋Š” ๊ทœ์น™์˜ ๊ธธ์ด๋Š” 6์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์ง„์ด๊ฐ€ ์ฐ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์ •๋‹ต์˜ ๊ธธ์ด๋ณด๋‹ค ์ปค์งˆ๋•Œ, break๋กœ ํƒˆ์ถœ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

String[] name

 - ๋งˆ์ง€๋ง‰ ์ถœ๋ ฅ์— ์‚ฌ์šฉํ•  ์„ธ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ์ €์žฅํ•œ๋‹ค.

 

int[] rightCount

 - ์„ธ ํ•™์ƒ์˜ ์ •๋‹ต ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

String answer

 - ์ •๋‹ต์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

 

/** 1 **/

์ƒ๊ทผ์ด๊ฐ€ ์ฐ์€ ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ๊ตฌํ•˜๋Š” for๋ฌธ์ด๋‹ค.

i๋Š” ์ •๋‹ต์˜ ๊ธธ์ด, j๋Š” ์ƒ๊ทผ์ด๊ฐ€ ์ฐ์€ ๋ฌธ์ž์˜ ๊ธธ์ด๋‹ค.

์œ„ flag๋ฅผ ์„ ์–ธํ• ๋•Œ ๋งํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ, ๋ฒ”์œ„๊ฐ€ ๋ฒ—์–ด๋‚ ๊ฒฝ์šฐ ํƒˆ์ถœ์„ ์‹œํ‚จ๋‹ค.

์ •๋‹ต์ด BAACC ์ผ ๋•Œ, ์ƒ๊ทผ์ด๋Š” A, B, C, A, B, C, ... ์ˆœ์œผ๋กœ ์ฐ๋Š”๋‹ค.

B A A

A B C

์œ„ ์„ธ๊ฐœ๋ฅผ ํ™•์ธํ•˜๊ณ , ๋„˜์–ด๊ฐ€์•ผํ•˜๋Š” ๋‹ค์Œ ์ •๋‹ต์€ ์ƒ๊ทผ์ด๊ฐ€ ์ฐ๋Š” ๊ทœ์น™์˜ ๊ธธ์ด๋งŒํผ ๋›ฐ์–ด๋„˜์–ด์•ผ ํ•œ๋‹ค.

B A A ๋‹ค์Œ ํ™•์ธํ•ด์•ผํ•  ๋ถ€๋ถ„์€ C C <- ์ด๋ถ€๋ถ„์ด๋‹ค.

๋”ฐ๋ผ์„œ i์˜ ๊ฐ’์„ A.length - 1 ๋งŒํผ ๋”ํ•ด์ฃผ๋Š”๋ฐ, -1์„ ํ•˜๋Š” ์ด์œ ๋Š” for๋ฌธ์— ++์„ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

B A A C C

A B C

๋‹ค์Œ์œผ๋กœ๋Š”

B A A C C

        A B C ... 

์œ„์™€๊ฐ™์ด ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ A๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” j๋กœ ๋™์ผํ•˜์ง€๋งŒ,

๋ฌธ์ž์—ด answer์˜ ์ธ๋ฑ์Šค๋Š” ์œ„์— ์ฒ˜๋Ÿผ ์ด๋™ํ•˜๋ฏ€๋กœ i๊ฐ’๋งŒํผ ๋”ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

if(answer.charAt(i+j) == A[j])
    rightCount[0] ++;

 

/** 2 **/ , /** 3 **/

์œ„ 2๋ฒˆ, 3๋ฒˆ์€ /**1 **/๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฐ๊ฐ ์ฐฝ์˜, ํ˜„์ง„์ด์˜ ์ •๋‹ต์„ ์นด์šดํŠธ ํ•ด์ฃผ๋Š” for๋ฌธ์ด๋‹คใ…ฃ.

 

 

/** 4 **/

์„ธ ํ•™์ƒ์ค‘ ์ •๋‹ต์„ ๊ฐ€์žฅ ๋งŽ์ด ๋งž์ถ˜(์ž˜์ฐ์€) ํ•™์ƒ์„ ์ฐพ๊ณ , max์— ์ €์žฅํ•œ๋‹ค.

 

 

/** 5 **/

๊ฐ ํ•™์ƒ๋ณ„ ๋งž์€ ์ •๋‹ต์„ ์ €์žฅํ•œ rightCount ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ’์ด max์™€ ๋™์ผํ•  ๋•Œ, 

๋™์ผํ•œ i๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ๊ฐ–๋Š” name ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

๋‹ค๋ฅธ ํ’€์ด

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = Integer.parseInt(sc.nextLine());
		String answer = sc.nextLine();
		int[] score = new int[3];
		int[] pattern = { 66, 65, 66, 67 };
		for (int i = 0; i < n; i++) {
			if (answer.charAt(i) == i % 3 + 65) {
				score[0]++;
			}
			if (answer.charAt(i) == pattern[i % 4]) {
				score[1]++;
			}
			if (answer.charAt(i) == (i + 4) % 6 / 2 + 65) {
				score[2]++;
			}
		}
		
		int max = 0;
		for (int i = 0; i < 3; i++) {
			if (score[i] > max) {
				max = score[i];
			}
		}
		
		System.out.println(max);
		if (score[0] == max) {
			System.out.println("Adrian");
		}
		if (score[1] == max) {
			System.out.println("Bruno");
		}
		if (score[2] == max) {
			System.out.println("Goran");
		}
		
		sc.close();
	}
}

pattern ๋ฐฐ์—ด์˜ 66, 65, 66, 67 ๊ฐ’์€ ๊ฐ๊ฐ ์•„์Šคํ‚ค์ฝ”๋“œ๊ฐ’์œผ๋กœ B, A, B, C ๊ฐ’์ด๋‹ค.

์œ„์— ๊ฐ ํ•™์ƒ๋ณ„ score ๋ฐฐ์—ด์„ ์ €์žฅํ•˜๋Š” ๋…ผ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด๋„ˆ๋ฌด ๊น”๋”ํ•˜๋‹ค ... !!

 

 

 

import java.util.*;

class Main {
	public static void main(String[] Q) {
		Scanner k = new Scanner(System.in);
		int s = Integer.parseInt(k.nextLine());
		String rslt = k.nextLine().substring(0, s);
		String a = "ABC";
		String b = "BABC";
		String g = "CCAABB";
		int best[] = new int[3];
		for (int i = 0; i < rslt.length(); i++) {
			if (rslt.charAt(i) == a.charAt(i % 3))best[0]++;
			if (rslt.charAt(i) == b.charAt(i % 4))best[1]++;
			if (rslt.charAt(i) == g.charAt(i % 6))best[2]++;
		}
		int Ma=best[0];
		int index=0;
		for(int i=0;i<best.length;i++) {
			if(best[i]>=Ma)
			{
				Ma=best[i];
				index=i;
			}
		}
		System.out.println(Ma);
		if(best[0]==Ma)System.out.println("Adrian");
		if(best[1]==Ma)System.out.println("Bruno");
		if(best[2]==Ma)System.out.println("Goran");

	}
}

๊ฐ ํ•™์ƒ์˜ ์ •๋‹ต์„ ์ €์žฅํ•  ๋•Œ, 3, 4, 6 ์œผ๋กœ ๋‚˜๋ˆˆ ์ธ๋ฑ์Šค์™€ ๋™์ผํ•œ์ง€๋ฅผ ํŒ๋‹จํ•ด์„œ ์ €์žฅ..... ์™•

if๋ฌธ์„ ํ•œ์ค„๋กœ ์„œ์ˆ ํ•˜๊ธด ํ–ˆ์ง€๋งŒ ์—ญ์‹œ ๊น”๋”ํ•œ ์ฝ”๋“œ ...

 

 

 

๋งˆ์ง€๋ง‰ ์ถœ๋ ฅ์€ ๋‹ค ๋™์ผํ•˜์ง€๋งŒ, ๊ฐ ํ•™์ƒ์ด ์ฐ๋Š” ๊ธธ์ด๋Š” 3, 4, 6 ์ด๋ฏ€๋กœ

์ด ๊ฐ’์„ ์ž˜ ์ด์šฉํ•˜๋ฉด ํ›จ์”ฌ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค .!!

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€