|
| 1 | +``` java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static int badLimit,len,ans; |
| 11 | + static boolean[] isBad; |
| 12 | + static char[] word; |
| 13 | + |
| 14 | + static class TrieNode{ |
| 15 | + HashMap<Character, TrieNode> map = new HashMap<>(); |
| 16 | + |
| 17 | + public void dfs(int badCnt) { |
| 18 | + if (badCnt > badLimit) { |
| 19 | + return; |
| 20 | + } |
| 21 | + ans++; |
| 22 | + |
| 23 | + for (char c: map.keySet()) { |
| 24 | + TrieNode child = map.get(c); |
| 25 | + |
| 26 | + if (isBad[c-'a']) child.dfs(badCnt+1); |
| 27 | + else child.dfs(badCnt); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + static TrieNode root = new TrieNode(); |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + public static void main(String[] args) throws IOException { |
| 40 | + init(); |
| 41 | + process(); |
| 42 | + print(); |
| 43 | + } |
| 44 | + |
| 45 | + public static void init() throws IOException { |
| 46 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 47 | + String rawWord = br.readLine(); |
| 48 | + len = rawWord.length(); |
| 49 | + word = new char[len]; |
| 50 | + |
| 51 | + for (int i = 0 ; i < len; i++) { |
| 52 | + word[i] = rawWord.charAt(i); |
| 53 | + } |
| 54 | + |
| 55 | + String[] rawBadGood = br.readLine().split(""); |
| 56 | + isBad = new boolean[26]; |
| 57 | + for (int i = 0; i < 26; i++) { |
| 58 | + isBad[i] = rawBadGood[i].equals("0"); |
| 59 | + } |
| 60 | + |
| 61 | + badLimit = Integer.parseInt(br.readLine()); |
| 62 | + ans = -1; |
| 63 | + } |
| 64 | + |
| 65 | + public static void process() { |
| 66 | + for (int i = 0; i < len; i++) { |
| 67 | + insert(i); |
| 68 | + } |
| 69 | + |
| 70 | + root.dfs(0); |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + public static void insert(int from) { |
| 77 | + TrieNode node =root; |
| 78 | + for (int i = from; i < len; i++) { |
| 79 | + char ch = word[i]; |
| 80 | + node = node.map.computeIfAbsent(ch, c -> new TrieNode()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + public static void print() { |
| 91 | + System.out.println(ans); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +``` |
0 commit comments