fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string s;
  5. string t[505];
  6. vector<int> pi[505];
  7. int mat[505];
  8. int dp[100005];
  9. int n;
  10. int main() {
  11. ios_base::sync_with_stdio(false);
  12. cin.tie(0);
  13. cin >> s >> n;
  14. for (int i = 0; i < n; ++i) {
  15. cin >> t[i];
  16. int m = 0;
  17. pi[i].assign(t[i].size(), 0);
  18. for (int j = 1; j < t[i].size(); ++j) {
  19. while (t[i][j] != t[i][m] && m > 0) m = pi[i][m - 1];
  20. if (t[i][j] == t[i][m]) ++m;
  21. pi[i][j] = m;
  22. }
  23. }
  24.  
  25. for (int i = 0; i < s.size(); ++i) {
  26. dp[i+1] = max(dp[i+1], dp[i]);
  27. for (int j = 0; j < n; ++j) {
  28. while (s[i] != t[j][mat[j]] && mat[j] > 0) mat[j] = pi[j][mat[j] - 1];
  29. if (s[i] == t[j][mat[j]]) ++mat[j];
  30. if (mat[j] == t[j].size()) {
  31. dp[i + 1] = max(dp[i + 1], dp[i - (int)t[j].size() + 1] + mat[j]);
  32. mat[j] = pi[j][mat[j] - 1];
  33. }
  34. }
  35. }
  36. cout << dp[s.size()] << '\n';
  37. }
  38.  
Success #stdin #stdout 0s 3868KB
stdin
aabcc
2
aab
bcc
stdout
3