• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 1339. 단어 수학

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int N,cnt;
	static int max=Integer.MIN_VALUE;
	static char alpha[]=new char[10];
	static char num[]=new char[10]; 
	static boolean replace[]=new boolean[10];
	static boolean check[]=new boolean [26];
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		N=sc.nextInt();
		String s=new String();
		char c[][]=new char[N][];		
		
		cnt=0;
		
		for (int i = 0; i < N; i++) {
			s=sc.next();
			int len=s.length();
			c[i]=new char[len];
			for (int j = 0; j < len; j++) {
				char a=s.charAt(j);
				c[i][j]=a;
				if(check[a-65]==false) alpha[cnt++]=a;
				check[a-65]=true;
			}
		}			
		
        for (int i=0; i<cnt; i++) {
            num[i] = (char)('9'-cnt+i+1);
        }
		
        
        do {
            int now = calc(num,c);
            if (max < now) {
                max = now;
            }
        } while(next_permutation(num));
		
		System.out.println(max);		
	}
	
    static boolean next_permutation(char[] num) {
        int i = cnt-1;
        while (i > 0 && num[i-1] >= num[i]) {
            i -= 1;
        }

        if (i <= 0) {
            return false;
        }

        int j = cnt-1;
        while (num[j] <= num[i-1]) {
            j -= 1;
        }

        char temp = num[i-1];
        num[i-1] = num[j];
        num[j] = temp;

        j = cnt-1;
        while (i < j) {
            temp = num[i];
            num[i] = num[j];
            num[j] = temp;
            i += 1;
            j -= 1;
        }
        return true;
    }
    
	public static int calc(char num[],char c[][]) {
		int result=0;
		for (int i = 0; i < c.length; i++) {
			int val=0;
			for (int j = 0; j < c[i].length; j++) {
				for (int k = 0; k < alpha.length; k++) {
					if(c[i][j]==alpha[k]) {
						val=val*10+(int)(num[k]-'0');
						break;
					}
				}
			}
			result+=val;
		}
		return result;
	}
    
}


브루트포스 Share