Last active
July 3, 2018 22:51
-
-
Save Gabrielgtt/c5339cf62c9e65c552f5c663eb6bc800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| #define MAXN 200000 | |
| #define debug_arr(arr, n) cerr << #arr << " = "; for (int i=0;i<n;i++) cout << arr[i] << " \n"[i == n-1] | |
| using namespace std; | |
| string frase, grande, pequena; | |
| int n, m, back[MAXN]; | |
| vector < vector <int> > dp; | |
| // aba?? | |
| // aba | |
| // 001 | |
| void kmp(){ | |
| for (int i=1, j; i<m; i++){ | |
| j = back[i-1]; | |
| while (j > 0 && pequena[i] != pequena[j]) j = back[j-1]; | |
| if (pequena[i] == pequena[j]) j++; | |
| back[i] = j; | |
| } | |
| debug_arr(back, m); | |
| } | |
| int solve(int p, int g){ | |
| cout << p << " " << g << endl; | |
| if (dp[p][g] != -1) return dp[p][g]; | |
| if (g > n) return dp[p][g] = 0; | |
| dp[p][g] = 0; | |
| if (p < m && (grande[g] == pequena[p] || grande[g] == '?')){ | |
| return dp[p][g] = solve(p + 1, g + 1); | |
| } | |
| int j = p; | |
| while (j > 0){ | |
| j = back[j-1]; | |
| dp[p][g] = max(dp[p][g], solve(j, g + 1)) + (p == m); | |
| } | |
| dp[p][g] = max(dp[p][g], solve(0, g + 1)); | |
| return dp[p][g]; | |
| } | |
| int main(){ | |
| ios::sync_with_stdio(false); | |
| cin.tie(NULL); | |
| cin >> grande >> pequena; | |
| n = grande.size(); | |
| m = pequena.size(); | |
| kmp(); | |
| for(int i = 0; i < m + 4; i++){ | |
| dp.push_back(vector<int>()); | |
| for(int j = 0; j < n + 4; j++){ | |
| dp[i].push_back(-1); | |
| } | |
| } | |
| cout << solve(0, 0) << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment