今日头条笔试 最大映射

/**
	 *给n个字符串
	 *这n个字符串全部由A-J的字母组成
	 *将A-j映射到0-9
	 *是的这n个字符串组成的值最大
	 *输出这个最大值
	 */
	
	
	import java.util.Scanner;
	
	public class Main {
		public static long ans = 0 ; 
		public static final int maxn = 100 ;
		public static long [] a= new long[maxn]  ;
		public static int n  ;
		private static boolean[] vis = new boolean[maxn];
		private static String[] s = new String[maxn]; 
		private static long[] ss = new long[maxn] ; 
		private static long[] temp = new long[maxn] ; 
		
		public static void judge(){
			long sum = 0 ;
			for(int i = 0;i < 10;i++){
				if(temp[i] == 1 && a[i] == 0)
					return  ; 
				sum += a[i]*ss[i] ; 
			}
			
			ans = Math.max(ans, sum) ; 
		}
		
		
		public static void dfs(int pos){
			if(pos == 10){
				judge() ; 
				return  ; 
			}
			for(int i = 9;i >= 0;i--){
				if(vis[i])continue ; 
				vis[i] = true ; 
				a[pos] = i ; 
				dfs(pos+1) ; 
				vis[i] = false ; 
			}
		}
		
		
		public static void main(String[] args) {
			Scanner in = new Scanner(System.in) ; 
			while(in.hasNextInt()){
				n = in.nextInt() ; 
				in.nextLine() ; 
				for(int i = 0;i < 10;i++){
					ss[i] = 0 ; 
					temp[i] = 0; 
				}
				
				for(int i = 0;i < n;i++){
					s[i] = in.nextLine() ;
					temp[s[i].charAt(0) - 'A'] = 1 ; 
					for(int j = 0;j < s[i].length();j++){
						ss[s[i].charAt(j)-'A'] += Math.pow(10, s[i].length()-j-1) ; 
					}
				}
				
				
				for(int i = 0;i < 10;i++){
					vis[i] = false ; 
				}
				dfs(0) ; 
				System.out.println(ans);
			}
			in.close();
		}
	}

你可能感兴趣的:(今日头条笔试 最大映射)