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
- #include "bits/stdc++.h"
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- #include "ext/rope"
-
- using namespace std;
- using namespace __gnu_pbds;
- using namespace __gnu_cxx;
-
- #define FAST ios_base::sync_with_stdio(false); cout.tie(nullptr); cout.tie(nullptr)
- #define PRECISION(t) cout << fixed << setprecision(t);
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
- #define REP(i, a, b) for (int i = a; i <= b; i++)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
- #define Rev(a) reverse(All(a))
- #define sz(a) (int)a.size()
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
-
-
- int dr[] = {1, -1, 0, 0};
- int dc[] = {0, 0, 1, -1};
-
-
-
-
-
- #define here cerr << "Here" << endl;
- #define trace1(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
- #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
- #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
- #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define ones(mask) __builtin_popcount(mask) // count set bit
- #define onesLL(mask) __builtin_popcountll(mask) // for long long
- #define lzeros(mask) __builtin_clz(mask) // no of leading zeros
- #define tzeros(mask) __builtin_ctz(mask) // no of trailing zeros
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 1e8
- #define mod 1000000007
-
- static const int maxn = 2e6 + 5;
- static const int logn = 18;
-
-
-
- typedef long double T;
- long double PI = acos(-1.0);
-
- struct Complex
- {
- T x, y;
- Complex(T x = 0, T y = 0) : x(x), y(y) {}
- Complex operator + (const Complex &a) const
- {
- return Complex(x + a.x, y + a.y);
- }
- Complex operator - (const Complex &a) const
- {
- return Complex(x - a.x, y - a.y);
- }
- Complex operator * (const Complex &a) const
- {
- return Complex(x*a.x - y*a.y, x*a.y + y*a.x);
- }
- };
-
- struct Fast_Fourier
- {
- Complex A[maxn];
- int rev(int id, int len)
- {
- int ret = 0;
- for (int i = 0; (1 << i) < len; i++)
- {
- ret <<= 1;
- if (id & (1 << i)) ret |= 1;
- }
- return ret;
- }
- void FFT(Complex a[], int len, int DFT)
- {
- for (int i = 0; i < len; i++) A[rev(i, len)] = a[i];
- for (int s = 1; (1 << s) <= len; s++)
- {
- int m = (1 << s);
- Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
- for (int k = 0; k < len; k += m)
- {
- Complex w = Complex(1, 0);
- for (int j = 0; j < (m >> 1); j++)
- {
- Complex t = w*A[k + j + (m >> 1)];
- Complex u = A[k + j];
- A[k + j] = u + t;
- A[k + j + (m >> 1)] = u - t;
- w = w * wm;
- }
- }
- }
- if (DFT == -1)
- {
- for (int i = 0; i < len; i++) A[i].x /= len, A[i].y /= len;
- }
- for (int i = 0; i < len; i++) a[i] = A[i];
- }
- } fft;
-
-
- string s, p;
- int len;
- ll res[maxn];
- Complex S[maxn], P[maxn], M[maxn];
- int ans[maxn];
-
- void work(char ch1, char ch2)
- {
- int n = sz(s);
- int m = sz(p);
- for (int i = 0; i < len; i++) S[i] = P[i] = Complex(0, 0);
- for (int i = 0; i < n; i++) S[i] = Complex(s[i] == ch1, 0);
- for (int i = 0; i < m; i++) P[m-1-i] = Complex(p[i] == ch2, 0);
- fft.FFT(S, len, 1);
- fft.FFT(P, len, 1);
- for (int i = 0; i < len; i++) M[i] = S[i] * P[i];
- fft.FFT(M, len, -1);
- for (int i = 0; i < len; i++) res[i] = (ll)(M[i].x + 0.5);
- for (int i = n; i < 2*n; i++) ans[i] += res[i];
- }
-
- int main()
- {
- FAST;
-
- #ifndef ONLINE_JUDGE
- freopen("in.txt", "r", stdin);
-
- #endif
-
- cin >> s;
- p = s;
- int n = sz(s);
- int m = sz(p);
- len = 1;
- if (n < m) swap(n, m);
- while (len <= n) len <<= 1;
- len <<= 1;
- assert(len < maxn);
- work('a', 'a');
- work('b', 'b');
- work('c', 'c');
- int maxMatch = 0;
- for (int i = n; i < 2*n; i++) maxMatch = max(maxMatch, ans[i]);
- cout << maxMatch << "\n";
- for (int i = n; i < 2*n; i++) if (maxMatch == ans[i]) cout << (i-n+1) << " ";
- }