(SPOJ) 1367 - Proteja sua senha - Solução 1

import java.io.*;
import java.util.*;

class Main {
    class Correspondencia {
        public int[] num = new int[11];
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.processa();
       
        System.exit(0);
    }
   
    void processa() throws NumberFormatException, IOException {
        String line = "";
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int contaCaso = 0;
        while ((line = br.readLine()) != null) {
            StringTokenizer tokenizer = new StringTokenizer(line);
            int qteLinhas = Integer.parseInt(tokenizer.nextToken());

            if (qteLinhas == 0) {
                return;
            }
           
            Correspondencia[] letras = new Correspondencia[10];
            for (int i = 0; i < 10; i++) {
                letras[i] = new Correspondencia();
            }
           
            String[] senha = new String[qteLinhas];
                               
            for (int i = 0; i < qteLinhas; i++) {
                line = br.readLine();
                tokenizer = new StringTokenizer(line);
                for (int j = 0; j < 10; j++) { // lê os números
                    int num = Integer.parseInt(tokenizer.nextToken());
                    letras[i].num[j] = num;
                }
                senha[i] = "";
                for (int j = 0; j < 6; j++) { // lê as letras
                    Character c = (tokenizer.nextToken()).charAt(0);
                    senha[i] += c;
                }
            }
           
            contaCaso++;
            System.out.println("Teste " + contaCaso);
            int a = 0;
            int resposta = 0;
            for (int j = 0; j < 6; j++) {
                int[] valor = new int[11];
                int maior = -1;
                for (int k = 0; k < qteLinhas; k++) {
                    char letra = senha[k].charAt(j);
                    if (letra == 'A') {
                        a = 0;
                    }
                    else if (letra == 'B') {
                        a = 2;
                    }
                    else if (letra == 'C') {
                        a = 4;
                    }
                    else if (letra == 'D') {
                        a = 6;
                    }
                    else if (letra == 'E') {
                        a = 8;
                    }

                    int contTmp = 0;
                    for (int i = a; contTmp < 2; i++) {
                        int indice = letras[k].num[i];
                        valor[indice]++;
                        if (valor[indice] > maior) {
                            maior = valor[indice];
                            resposta = indice;
                        }
                        contTmp++;
                    }
                }
                System.out.print(resposta + " ");
            }
            System.out.println("\n");  
        }
                      
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução