Thursday, February 28, 2019

[Spoj] MAXMATCH - Maximum Self-Matching

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : MAXMATCH - Maximum Self-Matching
Source            : Spoj
Category          : Linear Algebra
Algorithm         : Fast Fourier Transform
Verdict           : Accepted
Tutorial - https://codeforces.com/blog/entry/59386

  1. #include "bits/stdc++.h"  
  2. #include "ext/pb_ds/assoc_container.hpp"  
  3. #include "ext/pb_ds/tree_policy.hpp"  
  4. #include "ext/rope"  
  5.   
  6. using namespace std;  
  7. using namespace __gnu_pbds;  
  8. using namespace __gnu_cxx;  
  9.   
  10. #define FAST          ios_base::sync_with_stdio(false); cout.tie(nullptr); cout.tie(nullptr)  
  11. #define PRECISION(t)  cout << fixed << setprecision(t);  
  12.   
  13. #define FOR(i, n)       for (int i = 1; i <= n; i++)  
  14. #define For(i, n)       for (int i = 0; i < n; i++)  
  15. #define ROF(i, n)       for (int i = n; i >= 1; i--)  
  16. #define Rof(i, n)       for (int i = n-1; i >= 0; i--)  
  17. #define FORI(i, n)      for (auto i : n)  
  18. #define REP(i, a, b)    for (int i = a; i <= b; i++)  
  19.   
  20. #define ll              long long  
  21. #define ull             unsigned long long  
  22. #define vi              vector <int>  
  23. #define vl              vector <ll>  
  24. #define pii             pair <int, int>  
  25. #define pll             pair <ll, ll>  
  26. #define mk              make_pair  
  27. #define ff              first  
  28. #define ss              second  
  29. #define eb              emplace_back  
  30. #define em              emplace  
  31. #define pb              push_back  
  32. #define ppb             pop_back  
  33. #define All(a)          a.begin(), a.end()  
  34. #define memo(a, b)      memset(a, b, sizeof a)  
  35. #define Sort(a)         sort(All(a))  
  36. #define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())  
  37. #define Rev(a)          reverse(All(a))  
  38. #define sz(a)           (int)a.size()  
  39. #define max3(a, b, c)   max(a, max(b, c))  
  40. #define min3(a, b, c)   min(a, min(b, c))  
  41. #define maxAll(a)       *max_element(All(a))  
  42. #define minAll(a)       *min_element(All(a))  
  43. #define allUpper(a)     transform(All(a), a.begin(), :: toupper)  
  44. #define allLower(a)     transform(All(a), a.begin(), :: tolower)  
  45. #define endl            '\n'  
  46. #define nl              puts("")  
  47. #define ub              upper_bound  
  48. #define lb              lower_bound  
  49. #define Exp             exp(1.0)  
  50. #define PIE             2*acos(0.0)  
  51. #define Sin(a)          sin(((a)*PIE)/180.0)  
  52. #define EPS             1e-9  
  53.   
  54. template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;  
  55.   
  56. // rope <int> Rope;  
  57.   
  58. int dr[] = {1, -1, 0, 0}; // 4 Direction  
  59. int dc[] = {0, 0, 1, -1};  
  60. // int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction  
  61. // int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};  
  62. // int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves  
  63. // int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};  
  64.   
  65. #define here                                cerr << "Here" << endl;  
  66. #define trace1(x)                           cerr << #x << ": " << x << endl;  
  67. #define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;  
  68. #define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;  
  69. #define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;  
  70. #define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;  
  71. #define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;  
  72.   
  73. inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }  
  74. inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }  
  75. inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }  
  76. inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }  
  77.   
  78. #define ones(mask)                          __builtin_popcount(mask)   // count set bit  
  79. #define onesLL(mask)                        __builtin_popcountll(mask) // for long long  
  80. #define lzeros(mask)                        __builtin_clz(mask)        // no of leading zeros  
  81. #define tzeros(mask)                        __builtin_ctz(mask)        // no of trailing zeros  
  82.   
  83. inline int read()                           { int a; scanf("%d", &a); return a; }  
  84. inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }  
  85. inline double readDD()                      { double a; scanf("%lf", &a); return a; }  
  86.   
  87. template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }  
  88. int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }  
  89. ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }  
  90.   
  91. #define inf             1e8  
  92. #define mod             1000000007  
  93.   
  94. static const int maxn = 2e6 + 5;  
  95. static const int logn = 18;  
  96.   
  97.   
  98. // Fast Fourier Transform  
  99. typedef long double           T;  
  100. long double                   PI = acos(-1.0);  
  101.   
  102. struct Complex  
  103. {  
  104.       T x, y;  
  105.       Complex(T x = 0, T y = 0) : x(x), y(y) {}  
  106.       Complex operator + (const Complex &a) const  
  107.       {  
  108.             return Complex(x + a.x, y + a.y);  
  109.       }  
  110.       Complex operator - (const Complex &a) const  
  111.       {  
  112.             return Complex(x - a.x, y - a.y);  
  113.       }  
  114.       Complex operator * (const Complex &a) const  
  115.       {  
  116.             return Complex(x*a.x - y*a.y, x*a.y + y*a.x);  
  117.       }  
  118. };  
  119.   
  120. struct Fast_Fourier  
  121. {  
  122.       Complex A[maxn];  
  123.       int rev(int id, int len) // Reverse bit value 011 -> 110  
  124.       {  
  125.             int ret = 0;  
  126.             for (int i = 0; (1 << i) < len; i++)  
  127.             {  
  128.                   ret <<= 1;  
  129.                   if (id & (1 << i)) ret |= 1;  
  130.             }  
  131.             return ret;  
  132.       }  
  133.       void FFT(Complex a[], int len, int DFT)  
  134.       {  
  135.             for (int i = 0; i < len; i++) A[rev(i, len)] = a[i];  
  136.             for (int s = 1; (1 << s) <= len; s++)  
  137.             {  
  138.                   int m = (1 << s);  
  139.                   Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));  
  140.                   for (int k = 0; k < len; k += m)  
  141.                   {  
  142.                         Complex w = Complex(1, 0);  
  143.                         for (int j = 0; j < (m >> 1); j++)  
  144.                         {  
  145.                               Complex t = w*A[k + j + (m >> 1)];  
  146.                               Complex u = A[k + j];  
  147.                               A[k + j] = u + t;  
  148.                               A[k + j + (m >> 1)] = u - t;  
  149.                               w = w * wm;  
  150.                         }  
  151.                   }  
  152.             }  
  153.             if (DFT == -1)  
  154.             {  
  155.                   for (int i = 0; i < len; i++) A[i].x /= len, A[i].y /= len;  
  156.             }  
  157.             for (int i = 0; i < len; i++) a[i] = A[i];  
  158.       }  
  159. } fft;  
  160.   
  161.   
  162. string s, p;  
  163. int len;  
  164. ll res[maxn];  
  165. Complex S[maxn], P[maxn], M[maxn];  
  166. int ans[maxn];  
  167.   
  168. void work(char ch1, char ch2)  
  169. {  
  170.       int n = sz(s);  
  171.       int m = sz(p);  
  172.       for (int i = 0; i < len; i++) S[i] = P[i] = Complex(0, 0);  
  173.       for (int i = 0; i < n; i++) S[i] = Complex(s[i] == ch1, 0);  
  174.       for (int i = 0; i < m; i++) P[m-1-i] = Complex(p[i] == ch2, 0);  
  175.       fft.FFT(S, len, 1);  
  176.       fft.FFT(P, len, 1);  
  177.       for (int i = 0; i < len; i++) M[i] = S[i] * P[i];  
  178.       fft.FFT(M, len, -1);  
  179.       for (int i = 0; i < len; i++) res[i] = (ll)(M[i].x + 0.5);  
  180.       for (int i = n; i < 2*n; i++) ans[i] += res[i];  
  181. }  
  182.   
  183. int main()  
  184. {  
  185.       FAST;  
  186.       // PRECISION(15);  
  187.       #ifndef ONLINE_JUDGE  
  188.             freopen("in.txt""r", stdin);  
  189.             // freopen("out.txt", "w", stdout);  
  190.       #endif  
  191.   
  192.       cin >> s;  
  193.       p = s;  
  194.       int n = sz(s);  
  195.       int m = sz(p);  
  196.       len = 1;  
  197.       if (n < m) swap(n, m);  
  198.       while (len <= n) len <<= 1;  
  199.       len <<= 1;  
  200.       assert(len < maxn);  
  201.       work('a''a');  
  202.       work('b''b');  
  203.       work('c''c');  
  204.       int maxMatch = 0;  
  205.       for (int i = n; i < 2*n; i++) maxMatch = max(maxMatch, ans[i]);  
  206.       cout << maxMatch << "\n";  
  207.       for (int i = n; i < 2*n; i++) if (maxMatch == ans[i]) cout << (i-n+1) << " ";  
  208. }