(UVA) Memory Overflow - Solution 1

Brute Force was used to solve this problem.


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

class Main {
    public static void process() throws NumberFormatException, IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String line = br.readLine();
        int numTests = Integer.parseInt(line);
        for (int i = 0; i < numTests; i++) {
            line = br.readLine();
            String[] s = line.split("\\s");
           
            int numNames = Integer.parseInt(s[0]);
            int memory = Integer.parseInt(s[1]);
            String names = s[2];
           
            int answer = 0;
            StringBuilder recognize = new StringBuilder();
            for (int j = 0; j < names.length(); j++) {
                if (j < memory) {
                    if (recognize.indexOf(names.charAt(j)+"", 0) != -1) {
                        answer++;
                    }
                }
                else {
                    if (recognize.indexOf(names.charAt(j)+"", j-memory) != -1) {
                        answer++;
                    }
                }
                recognize.append(names.charAt(j));
            }
           
            System.out.println("Case " + (i+1) + ": " + answer);
        }
                   
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução