[BOJ] 백준 [1339] 단어 수학JAVA

[BOJ] 백준 [1339] 단어 수학JAVA

import java.util. * ;

import java.io. * ;

public class Main{

static ArrayList < Character > list = new ArrayList < Character > ();

static String [] input;

static boolean [] visit;

static int [] value;

static int N,max;

public static void main( String [] args) throws IOException {

BufferedReader br = new BufferedReader( new InputStreamReader( System . in ));

N = Integer. parseInt (br.readLine());

input = new String [N];

for ( int i = 0 ;i < N;i + + ) {

input[i] = br.readLine();

for ( char c : input[i].toCharArray())

if ( ! list.contains(c)) list. add (c);

}

visit = new boolean [ 10 ];

value = new int [list.size()];

dfs( 0 );

System . out . println (max);

}

private static void dfs( int depth) {

if (depth = = list.size()){

int sum = 0 ;

for ( int i = 0 ;i < input. length ;i + + ){

int num = 0 ;

for ( int j = 0 ;j < input[i]. length ();j + + ){

num * = 10 ;

num + = value[list. indexOf (input[i]. charAt (j))];

}

sum + = num;

}

max = Math.max(max,sum);

return ;

}

for ( int i = 0 ;i < = 9 ;i + + ){

if (visit[i]) continue ;

visit[i] = true ;

value[depth] = i;

dfs(depth + 1 );

value[depth] = 0 ;

visit[i] = false ;

}

}

}

from http://katastrophe.tistory.com/60 by ccl(A) rewrite - 2021-10-25 17:27:42