Guest User

Untitled

a guest
Jul 5th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import static java.lang.Integer.max;
  6. /**
  7.  * Created by bugkiller on 7/1/2017.
  8.  */
  9. class MonkAndChocolates {
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14.         String s[],chocolateString;
  15.        
  16.         int testCases,magicMoves,chocolates;
  17.  
  18.         testCases = Integer.parseInt(br.readLine());
  19.         while (testCases-- > 0) {
  20.             s = br.readLine().split("\\s");
  21.             chocolates = Integer.parseInt(s[0]);
  22.             magicMoves = Integer.parseInt(s[1]);
  23.             chocolateString = br.readLine();
  24.             System.out.println(solve(chocolates, magicMoves, chocolateString));
  25.         }
  26.     }
  27.  
  28.     private static int solve(int chocolates, int magicMoves, String chocolateString) {
  29.  
  30.         int ans = 0;
  31.  
  32.         for (int c = 0; c < 26; ++c) {
  33.             int letterCount = 0,nonLetterCount = 0;
  34.             int remove = 0;
  35.             for (int k = 0; k < chocolateString.length(); ++k) {
  36.  
  37.                 if(chocolateString.charAt(k) == (char)(c + 'a')){
  38.                     letterCount++;
  39.                 }
  40.                 else
  41.                     nonLetterCount++;
  42.  
  43.                 if (nonLetterCount <= magicMoves) {
  44.                     ans = max(ans, nonLetterCount + letterCount);
  45.                 }
  46.                 else {
  47.                     if(chocolateString.charAt(remove++) == (char) (c + 'a')){
  48.                         letterCount--;
  49.                     }
  50.                     else
  51.                         nonLetterCount--;
  52.                 }
  53.             }
  54.  
  55.         }
  56.         for (int c = 0; c < 26; ++c) {
  57.             int letterCount = 0, nonLetterCount = 0;
  58.             int remove = 0;
  59.             for (int k = 0; k < chocolateString.length(); ++k) {
  60.                 if(chocolateString.charAt(k) ==(char) (c + 'A')){
  61.                     letterCount++;
  62.                 }
  63.                 else
  64.                     nonLetterCount++;
  65.  
  66.                 if (nonLetterCount <= magicMoves) {
  67.                     ans = max(ans, nonLetterCount + letterCount);
  68.                 }
  69.                 else {
  70.                     if(chocolateString.charAt(remove++) ==(char) (c + 'A')){
  71.                         letterCount--;
  72.                     }
  73.                     else
  74.                         nonLetterCount--;
  75.                 }
  76.             }
  77.  
  78.         }
  79.         return ans;
  80.     }
  81. }
Add Comment
Please, Sign In to add comment