fork(15) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. template <int delta> struct ManacherBase{
  7. private:
  8. static const int maxn=1e5+1;
  9. int r[maxn];
  10. char s[maxn];
  11. int mid,n,i;
  12.  
  13. public:
  14. ManacherBase():mid(0),i(0),n(1)
  15. {
  16. memset(r,-1,sizeof(int)*maxn);
  17. s[0]='$';
  18. r[0]=0;
  19. }
  20.  
  21. int get(int pos)
  22. {
  23. pos++;
  24. if(pos<=mid)
  25. return r[pos];
  26. else
  27. return min(r[mid - (pos - mid)], n - pos - 1);
  28. }
  29.  
  30. void addLetter(char c)
  31. {
  32. s[n]=s[n+1]=c;
  33.  
  34. while(s[i - r[i] - 1 + delta] != s[i + r[i] + 1])
  35. r[++i] = get(i-1);
  36. r[mid=i]++, n++;
  37. }
  38.  
  39. int maxPal()
  40. {
  41. return ( n - mid - 1 ) * 2 + 1 - delta;
  42. }
  43. } ;
  44.  
  45. struct Manacher{
  46. private:
  47. ManacherBase<1> manacherEven;
  48. ManacherBase<0> manacherOdd;
  49. public:
  50. void addLetter(char c)
  51. {
  52. manacherEven.addLetter(c);
  53. manacherOdd.addLetter(c);
  54. }
  55.  
  56. int maxPal()
  57. {
  58. return max(manacherEven.maxPal(), manacherOdd.maxPal());
  59. }
  60. int getRad(int type,int pos)
  61. {
  62. if(type)
  63. return manacherOdd.get(pos);
  64. else
  65. return manacherEven.get(pos);
  66. }
  67. } ;
  68.  
  69. main()
  70. {
  71. ios::sync_with_stdio(0);
  72. cin.tie(0);
  73. string t;
  74. while(cin>>t)
  75. {
  76. int n=t.size();
  77. Manacher test;
  78. for(int i=0;i<n;i++)
  79. {
  80. test.addLetter(t[i]);
  81. cout<<"Arrays for string "<<t.substr(0,i+1)<<":"<<endl;
  82. for(int z=0;z<2;z++)
  83. {
  84. for(int j=0;j<=i;j++)
  85. cout<<test.getRad(z,j)<<' ';
  86. cout<<endl;
  87. }
  88. }
  89. cout<<"========================================="<<endl;
  90. }
  91. }
  92.  
  93.  
Success #stdin #stdout 0s 4336KB
stdin
aaaaaaaaaaaaaaaaaaaa
stdout
Arrays for string a:
0 
0 
Arrays for string aa:
1 0 
0 0 
Arrays for string aaa:
1 1 0 
0 1 0 
Arrays for string aaaa:
1 2 1 0 
0 1 1 0 
Arrays for string aaaaa:
1 2 2 1 0 
0 1 2 1 0 
Arrays for string aaaaaa:
1 2 3 2 1 0 
0 1 2 2 1 0 
Arrays for string aaaaaaa:
1 2 3 3 2 1 0 
0 1 2 3 2 1 0 
Arrays for string aaaaaaaa:
1 2 3 4 3 2 1 0 
0 1 2 3 3 2 1 0 
Arrays for string aaaaaaaaa:
1 2 3 4 4 3 2 1 0 
0 1 2 3 4 3 2 1 0 
Arrays for string aaaaaaaaaa:
1 2 3 4 5 4 3 2 1 0 
0 1 2 3 4 4 3 2 1 0 
Arrays for string aaaaaaaaaaa:
1 2 3 4 5 5 4 3 2 1 0 
0 1 2 3 4 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaa:
1 2 3 4 5 6 5 4 3 2 1 0 
0 1 2 3 4 5 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaa:
1 2 3 4 5 6 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaa:
1 2 3 4 5 6 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 7 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0 
Arrays for string aaaaaaaaaaaaaaaaaaaa:
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 
=========================================