Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <unordered_map>
  4. #include <cmath>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. struct Node {
  10.     int terminate = 0;
  11.     unordered_map<char, int> next;
  12. };
  13.  
  14. int index = 1;
  15. vector<Node> bor;
  16.  
  17. int d;
  18. int maxLength = 0;
  19. vector<vector<int>> dp;
  20.  
  21. int currentAns;
  22.  
  23. void addString(string& str) {
  24.     int currentIndex = 0;
  25.     for (int i = 0; i < str.size(); ++i) {
  26.         if (bor[currentIndex].next.count(str[i]) == 0) {
  27.             Node newNode;
  28.             bor.push_back(newNode);
  29.             bor[currentIndex].next[str[i]] = index;
  30.             index++;
  31.         }
  32.  
  33.         currentIndex = bor[currentIndex].next[str[i]];
  34.     }
  35.  
  36.     bor[currentIndex].terminate++;
  37. }
  38.  
  39. void checkBor(int currentIndex) {
  40.     cout << currentIndex << " ";
  41.     for (auto x : bor[currentIndex].next) {
  42.         cout << x.first << " ";
  43.     }
  44.     cout << bor[currentIndex].terminate << "\n";
  45.  
  46.     for (auto x : bor[currentIndex].next) {
  47.         checkBor(x.second);
  48.     }
  49. }
  50.  
  51. void countCurrentAns(int currentIndex, string& str, int l, int r, int lastL, int lastR, int len, char ch) {
  52.     bool isEnd = true;
  53.  
  54. //    cout << currentIndex << " " << l << " " << r << " " << lastL << " " << lastR << " " << len << " " << ch << "\n";
  55.     for (int i = l; i <= r; ++i) {
  56.         if (len == 0) {
  57.             if (i == l) {
  58.                 dp[len][0] = 0;
  59.             }
  60.             else {
  61.                 dp[len][i - l] = dp[len][i - l - 1] + 1;
  62.             }
  63.  
  64.             isEnd = false;
  65.         }
  66.         else {
  67.             int left = 1e9;
  68.             if (i != l) {
  69.                 left = dp[len][i - l - 1] + 1;
  70.             }
  71.  
  72.             int up = 1e9;
  73.             if (lastL <= i && i <= lastR) {
  74.                 up = dp[len - 1][i - lastL] + 1;
  75.             }
  76.  
  77.             int diag = 1e9;
  78.             if (lastL <= (i - 1) && (i - 1) <= lastR) {
  79.                 diag = dp[len - 1][(i - 1) - lastL] + 1;
  80.                 if (str[i - 1] == ch) {
  81.                     diag--;
  82.                 }
  83.             }
  84.  
  85.             dp[len][i - l] = min(left, min(up, diag));
  86.             if (dp[len][i - l] <= d) {
  87.                 isEnd = false;
  88.             }
  89.         }
  90.  
  91. //        cout << dp[len][i - l] << " ";
  92.     }
  93. //    cout << "\n\n";
  94.  
  95.     if (r == str.size() && dp[len][r - l] <= d) {
  96.         currentAns += bor[currentIndex].terminate;
  97.     }
  98.  
  99.     if (!isEnd) {
  100.         for (auto x : bor[currentIndex].next) {
  101.             countCurrentAns(x.second, str, (len <= d ? 0 : l + 1), (r == str.size() ? r : r + 1), l, r, len + 1, x.first);
  102.         }
  103.     }
  104. }
  105.  
  106. int main() {
  107.     ios_base::sync_with_stdio(false);
  108.  
  109.     Node root;
  110.     bor.push_back(root);
  111.  
  112.     string str;
  113.     int n;
  114.     cin >> n;
  115.     for (int i = 0; i < n; ++i) {
  116.         cin >> str;
  117.         addString(str);
  118.  
  119.         maxLength = max(maxLength, (int)str.size());
  120.     }
  121.  
  122.     cin >> d;
  123.     dp = vector<vector<int>>(maxLength + 1, vector<int>(d*2 + 1));
  124.  
  125.     int k;
  126.     cin >> k;
  127.     for (int i = 0; i < k; ++i) {
  128.         cin >> str;
  129.  
  130.         currentAns = 0;
  131.         countCurrentAns(0, str, 0, min(d, (int)str.size()), -1, -1, 0, '*');
  132.  
  133.         cout << currentAns << "\n";
  134.     }
  135.    
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement