Advertisement
Korotkodul

ЗОШ основание строки тесты

Jan 11th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 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 S; cin>>S; int n = S.size();
  37. vector <int> P(n, 0);
  38. for (int i = 1; i < n;++i){
  39. int j = P[i-1];
  40. while (j > 0 && S[j] != S[i]){
  41. j = P[j-1];
  42. }
  43. if (S[i] == S[j]){
  44. j++;
  45. }
  46. P[i] = j;
  47. }
  48. cv(P);
  49. //cout<<count(P.begin(), P.end(), 0);
  50. //cout<<*upper_bound(P.begin(), P.end(), 0) - *lower_bound(P.begin(), P.end(), 0);
  51. /*int fr, to;
  52. for (int i =0;i<n;++i){
  53. if (P[i] == 0){
  54. fr=i;
  55. break;
  56. }
  57. }
  58. for (int i = fr+1;i<n;++i){
  59. if (P[i]==0){
  60. to=i;
  61. break;
  62. }
  63. }
  64.  
  65. cout<<to-fr+1;*/
  66. }
  67. /*
  68. Контрпример:
  69.  
  70. abbbbbaaaa
  71. 6
  72.  
  73. aabbbbbaaaa
  74.  
  75. candalacadndala
  76. 7
  77.  
  78. kalkulkalkulkalkulkalka
  79.  
  80.  
  81. lkulkalkulka
  82.  
  83. akkakakkakakka
  84. */
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement