Guest User

Untitled

a guest
Dec 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class bruteforce {
  4.  
  5.     static String word, dict;
  6.  
  7.     static boolean solve(int i1, int i2, int depth) {
  8.         if (depth < 0)
  9.             return false;
  10.  
  11.         int l1 = word.length(), l2 = dict.length();
  12.  
  13.         if (i1 == l1)
  14.             return true;
  15.  
  16.         if (i2 >= l2)
  17.             return solve(i1, i2 + 1, depth - 1);
  18.         else
  19.             return solve(i1 + 1, i2 + 1,
  20.                     depth - (word.charAt(i1) == dict.charAt(i2) ? 0 : 1))
  21.                     || solve(i1 + 1, i2, depth - 1)
  22.                     || solve(i1, i2 + 1, depth - 1);
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Scanner myScanner = new Scanner(System.in);
  27.         int d = myScanner.nextInt();
  28.         String[] dic = new String[d];
  29.         for (int i = 0; i < dic.length; i++)
  30.             dic[i] = myScanner.next();
  31.         int q = myScanner.nextInt();
  32.         for (int i = 0; i < q; i++) {
  33.             word = myScanner.next();
  34.             int res = 0;
  35.             int depth = myScanner.nextInt();
  36.             for (int j = 0; j < dic.length; j++) {
  37.                 dict = dic[j];
  38.                 res += solve(0, 0, depth) ? 1 : 0;
  39.             }
  40.             System.out.println(res);
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment