Submission #1209104

#TimeUsernameProblemLanguageResultExecution timeMemory
1209104zyadhanySateliti (COCI20_satellti)C++20
0 / 110
271 ms589824 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

#define ll int
#define ld long double
#define pl pair<ll, ll>
#define vi vector<ll>
#define vii vector<vi>
#define vc vector<char>
#define vcc vector<vc>
#define vp vector<pl>
#define mi map<ll,ll>
#define mc map<char,int>
#define sortx(X) sort(X.begin(),X.end());
#define all(X) X.begin(),X.end()
#define allr(X) X.rbegin(),X.rend()
#define ln '\n'
#define YES {cout << "YES\n"; return;}
#define NO {cout << "NO\n"; return;}
#define MUN {cout << "-1\n"; return;}
using namespace std;


class HashedString {
  public:
	// change M and B if you want
	static const ll M = (1LL << 61) - 1;
	static const ll B;
	static __int128 mul(ll a, ll b) { return (__int128)a * b; }
	static ll mod_mul(ll a, ll b) { return mul(a, b) % M; }

  private:
	// pow[i] contains P^i % M
	static vector<ll> pow;
	// p_hash[i] is the hash of the first i characters of the given string
	vector<ll> p_hash;
  public:
	HashedString(const string &s) : p_hash(s.size() + 1) {
		while (pow.size() < s.size()) { pow.push_back(mod_mul(pow.back(), B)); }
		p_hash[0] = 0;
		for (int i = 0; i < s.size(); i++) {
			p_hash[i + 1] = (mul(p_hash[i], B) + s[i]) % M;
		}
	}

	ll get_hash(int start, int end) {
		ll raw_val = p_hash[end + 1] - mod_mul(p_hash[start], pow[end - start + 1]);
		return (raw_val + M) % M;
	}
};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
vector<ll> HashedString::pow = {1};
const ll HashedString::B = uniform_int_distribution<ll>(0, M - 1)(rng);
// EndCodeSnip

const auto M = HashedString::M;
const auto B = HashedString::B;
const auto mul = HashedString::mul;
const auto mod_mul = HashedString::mod_mul;
ll inv(ll base, ll MOD) {
	ll ans = 1, expo = MOD - 2;
	while (expo) {
		if (expo & 1) { ans = mod_mul(ans, base); }
		expo >>= 1;
		base = mod_mul(base, base);
	}
	return ans;
}

vi suffixarray(vi &s, ll m) {
    int n = s.size();
    vi suff(n), P(n);
    vector<pair<int, int>> V(n);
    for (int i = 0; i < n; i++) suff[i]=i, V[i] = {s[i], s[i]};
    
    auto comp = [&](int i, int j) {
        if (V[i].first != V[j].first) return V[i].first < V[j].first;
        return V[i].second < V[j].second;
    };
    sort(all(suff), comp);
    
    vi tmp(n), frq(n), C(n);
    for (int k = 1; k < n; k*=2)
    {
        C[0] = frq[0] = P[suff[0]] = 0;
        for (int i = 1; i < n; i++) P[suff[i]] = P[suff[i-1]] + (V[suff[i]] != V[suff[i-1]]), frq[i] = 0;
        for (int i = 0; i < n; i++) V[i] = {P[i], P[(i+k*m)%n]}, suff[i] = ((suff[i] - m*k)%n + n) % n, frq[P[i]]++;
        for (int i = 1; i < n; i++) C[i] = C[i-1] + frq[i-1];
        for (int i = 0; i < n; i++) tmp[C[V[suff[i]].first]] = suff[i], C[V[suff[i]].first]++;
        swap(suff, tmp);
    }

    return suff;
}


void solve(int tc) {
	ll n, m;

	cin >> n >> m;

	vector<string> X(n);
	for (int i = 0; i < n; i++)
	{
		cin >> X[i];
		for (auto &c : X[i]) if (c=='.') c = (char)2;
		else c = (char)1;
	}

	vi suff(m*n);
	for (int i = 0; i < n*m; i++) suff[i] = i;
	vector<HashedString> H;

	for (auto &s : X) {
		s += s;
		H.push_back(HashedString(s));	
	}
	
	auto cmp = [&](ll i, ll j) {
		ll l = 0, r = m;
		while (l < r)
		{
			ll mid = (l + r + 1) / 2;
			ll hi = H[i/m].get_hash(i%m, i%m+mid-1);
			ll hj = H[j/m].get_hash(j%m, j%m+mid-1);
			if (hi == hj) l = mid;
			else r = mid - 1;
		}
		
		if (l == m) return false;
		return X[i/m][i%m+l] < X[j/m][j%m+l];
	};
	sort(suff.begin(), suff.end(), cmp);

	vi V(n*m);
	V[suff[0]] = 0;
	for (int i = 1; i < suff.size(); i++)
	{
		ll hi = H[suff[i]/m].get_hash(suff[i]%m, suff[i]%m+m-1);
		ll hj = H[suff[i-1]/m].get_hash(suff[i-1]%m, suff[i-1]%m+m-1);
		V[suff[i]] = V[suff[i-1]] + (hi!=hj);
	}
	
	suff = suffixarray(V, m);
	
	ll star = suff[0]/m, sh = suff[0]%m;
	for (int i = 0; i < n; i++)
	{
		X[i] += X[i].substr(0, sh);
		X[i] = X[i].substr(sh, m);
		for (auto &c : X[i]) if (c == 2) c = '.';
		else c = '*';
	}
	for (int i = 0; i < n; i++) cout << X[(i+star)%n] << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int size = 1;    

    // freopen("lightsout.in", "r", stdin);
    // freopen("lightsout.out", "w", stdout);

    // cin >> size;
    for (int i = 1; i <= size ; i++) solve(i);
    return 0;
}

Compilation message (stderr)

Main.cpp:29:41: warning: overflow in conversion from 'long long int' to 'int' changes value from '2305843009213693951' to '-1' [-Woverflow]
   29 |         static const ll M = (1LL << 61) - 1;
      |                             ~~~~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...