Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class bruteforce {
- static String word, dict;
- static boolean solve(int i1, int i2, int depth) {
- if (depth < 0)
- return false;
- int l1 = word.length(), l2 = dict.length();
- if (i1 == l1)
- return true;
- if (i2 >= l2)
- return solve(i1, i2 + 1, depth - 1);
- else
- return solve(i1 + 1, i2 + 1,
- depth - (word.charAt(i1) == dict.charAt(i2) ? 0 : 1))
- || solve(i1 + 1, i2, depth - 1)
- || solve(i1, i2 + 1, depth - 1);
- }
- public static void main(String[] args) {
- Scanner myScanner = new Scanner(System.in);
- int d = myScanner.nextInt();
- String[] dic = new String[d];
- for (int i = 0; i < dic.length; i++)
- dic[i] = myScanner.next();
- int q = myScanner.nextInt();
- for (int i = 0; i < q; i++) {
- word = myScanner.next();
- int res = 0;
- int depth = myScanner.nextInt();
- for (int j = 0; j < dic.length; j++) {
- dict = dic[j];
- res += solve(0, 0, depth) ? 1 : 0;
- }
- System.out.println(res);
- }
- }
- }
Add Comment
Please, Sign In to add comment