Guest User

Untitled

a guest
Apr 24th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Problem4 {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         String input = sc.nextLine();
  9.         String pattern = sc.nextLine();
  10.  
  11.         Pattern pat = Pattern.compile(pattern);
  12.         int groupcount = 0;
  13.         while (true) {
  14.             Matcher match = pat.matcher(input);
  15.             if(pattern.equals("")){
  16.                 System.out.println("No shake.");
  17.                 break;
  18.             }
  19.             while (match.find()) {
  20.                 groupcount++;
  21.             }
  22.             if (groupcount < 2) {
  23.                 System.out.println("No shake.");
  24.                 break;
  25.             }
  26.             groupcount=0;
  27.             input = input.replaceFirst(pattern, "");
  28.             input = replaceLast(input, pattern, "");
  29.             int indexToRemove = pattern.length() / 2;
  30.             pattern = pattern.substring(0, indexToRemove) + pattern.substring(indexToRemove + 1);
  31.             System.out.println("Shaked it.");
  32.             pat = Pattern.compile(pattern);
  33.         }
  34.         System.out.println(input);
  35.     }
  36.  
  37.     public static String replaceLast(String string, String toReplace, String replacement) {
  38.         int pos = string.lastIndexOf(toReplace);
  39.         if (pos > -1) {
  40.             return string.substring(0, pos)
  41.                     + replacement
  42.                     + string.substring(pos + toReplace.length(), string.length());
  43.         } else {
  44.             return string;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment