Advertisement
Korotkodul

ЗОШ Z-функция OK

Jan 11th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. using db = double;
  15. void cv(vector <int> &v){
  16. for (auto x: v) cout<<x<<' ';
  17. cout<<"\n";
  18. }
  19.  
  20. void cvl(vector <ll> &v){
  21. for (auto x: v) cout<<x<<' ';
  22. cout<<"\n";
  23. }
  24.  
  25.  
  26. void cvv(vector <vector <int> > &v){
  27. for (auto x: v) cv(x);
  28. cout<<"\n";
  29. }
  30.  
  31. int main()
  32. {
  33. ios::sync_with_stdio(0);
  34. cin.tie(0);
  35. cout.tie(0);
  36. string a,b; cin>>a>>b;
  37. int n = a.size() + b.size() + 1;
  38. string S = b + "#" + a;
  39. vector <int> Z(n, 0);
  40. pii lr = {-2e9, -2e9};
  41. int l,r;
  42. //cout<<"S = "<<S<<"\n";
  43. for (int i = 1; i < n;++i){
  44. if (i <= lr.second){
  45. Z[i] = min(lr.second - i + 1, Z[i - lr.first ]);
  46. }
  47. while (i + Z[i] < n && S[i + Z[i]] == S[Z[i]]){
  48. ++Z[i];
  49. }
  50. r = i + Z[i] - 1;
  51. l = i;
  52. if (r > lr.second){
  53. lr = {l, r};
  54. }
  55. }
  56. vector <int> ans;
  57. for (int i = 0; i < n;++i){
  58. if (Z[i] == b.size()){
  59. ans.push_back(i - (b.size() + 1) + 1);
  60. }
  61. }
  62. cout<<ans.size()<<"\n";
  63. for (auto x: ans){
  64. cout<<x<<' ';
  65. }
  66. //cv(Z);
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement