text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
long long p[1000005];
long long mark[1000005];
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long i, j, n, m, t;
cin >> s;
for (i = 1, j = 0; i < s.size(); i++) {
while (j && s[i] != s[j]) j = p[j - 1];
if (s[i] == s[j]) {
j++;
if (i != s.size() - 1) mark[j] = 1;
}
p[i] = j;
}
while (j && mark[j] != 1) j = p[j - 1];
if (j)
cout << s.substr(0, j);
else
cout << "Just a legend" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
string txt;
int z[2000009];
void zFunc(string s) {
int n = (int)s.size();
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
}
string s;
set<int> ss;
int main() {
cin >> s;
zFunc(s);
int n = s.size();
int ans = 0;
for (int i = 01; i < n; i++) {
if (i + z[i] == n) {
if (i <= z[i]) {
int cur = z[i] - i;
ans = max(ans, cur);
} else
ss.insert(z[i]);
}
}
if (ss.size())
for (int i = 1; i < n; i++) {
if (z[i] + i != n) {
if (z[i] >= *ss.begin()) ans = max(ans, *(--ss.upper_bound(z[i])));
}
}
if (!ans)
cout << "Just a legend\n";
else
cout << s.substr(0, ans) << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
const int INF = (int)1e7;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
void computeLPSArray(string pat, int M, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
int main() {
string txt, pat;
long long i, j, k, M, ans;
cin >> pat;
M = pat.size();
int lps[M];
computeLPSArray(pat, M, lps);
ans = lps[M - 1];
if (ans == 0) {
cout << "Just a legend" << endl;
return 0;
}
for (i = 1; i < M - 1; i++) {
if (lps[i] == ans) {
cout << pat.substr(0, ans) << endl;
return 0;
}
}
if (lps[ans - 1]) {
cout << pat.substr(0, lps[ans - 1]);
} else {
cout << "Just a legend" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, i, A[1000001], t;
vector<int> Zfunction(string s) {
n = s.size();
vector<int> Z(n, 0);
int l = 0, r = 0;
for (i = 1; i < n; i++) {
if (i <= r) Z[i] = min(r - i + 1, Z[i - l]);
while (i + Z[i] < n && s[Z[i]] == s[i + Z[i]]) Z[i]++;
if (i + Z[i] - 1 > r) r = i + Z[i] - 1, l = i;
}
return Z;
}
int main() {
string s;
cin >> s;
vector<int> Z(Zfunction(s));
for (i = 1; i < n; i++) {
if (Z[i] == n - i && t >= Z[i]) {
cout << s.substr(0, Z[i]);
return 0;
} else
t = max(t, Z[i] == n - i ? Z[i] - 1 : Z[i]);
}
cout << "Just a legend";
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
long long pw(long long b, long long p) {
if (!p) return 1;
long long sq = pw(b, p / 2) % 1000000007;
sq = (sq * sq) % 1000000007;
if (p % 2) sq = (sq * b) % 1000000007;
return sq;
}
const int N = 1e6 + 10, M = 0;
int Zarr[N];
string s;
void getZarr() {
memset(Zarr, 0, sizeof Zarr);
int n = (int)s.size();
int r, l, k;
l = r = 0;
for (int i = 1; i < n; i++) {
if (i > r) {
l = r = i;
while (s[r - l] == s[r] && r < n) r++;
Zarr[i] = r - l;
r--;
} else {
k = i - l;
if (Zarr[k] < r - i + 1) {
Zarr[i] = Zarr[k];
} else {
l = i;
while (r < n && s[r - l] == s[r]) r++;
Zarr[i] = r - l;
r--;
}
}
}
}
int mp[N], ex[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
getZarr();
int mx = 0;
bool ok = 0;
int t = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (Zarr[i] && ((Zarr[i + Zarr[i]] == Zarr[i]) ||
((i + Zarr[i] != (int)s.size()) ||
((i + Zarr[i] == (int)s.size() && ok))))) {
if (Zarr[(int)s.size() - Zarr[i]] == Zarr[i] && (t >= Zarr[i])) {
mx = max(mx, Zarr[i]);
}
}
if (Zarr[i]) ok = 1;
t = max(t, Zarr[i]);
}
if (!mx) {
cout << "Just a legend";
return 0;
}
string res = s.substr(0, mx);
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m = 998244353) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
long long inverse(long long a, long long m = 998244353) {
return power(a, m - 2, m);
}
vector<int> compZ(string str) {
int n = str.length();
vector<int> z(n, 0);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && str[i + z[i]] == str[z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
int main() {
string str;
cin >> str;
int n = str.length();
int mx = 0;
set<int> ss1, ss2;
vector<int> z = compZ(str);
for (int i = 1; i < n; i++) {
if (i + z[i] - 1 == n - 1) {
mx = max(mx, z[i] - 1);
ss2.insert(z[i]);
} else
mx = max(mx, z[i]);
}
int ans = -1;
for (int x : ss2) {
if (x <= mx) ans = x;
}
if (ans == -1)
cout << "Just a legend" << endl;
else {
for (int i = 0; i < ans; i++) cout << str[i];
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long infinity = 1000000000000000000;
bool do_debug = false;
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto element : v) {
os << element << " ";
}
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& v) {
if (v.size() == 0) {
os << "empty set\n";
return os;
}
auto endit = v.end();
endit--;
os << "[";
for (auto it = v.begin(); it != v.end(); it++) {
os << *it;
if (it != endit) {
os << ", ";
}
}
os << "]";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, multiset<T>& v) {
if (v.size() == 0) {
os << "empty multiset\n";
return os;
}
auto endit = v.end();
endit--;
os << "[";
for (auto it = v.begin(); it != v.end(); it++) {
os << *it;
if (it != endit) {
os << ", ";
}
}
os << "]";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, map<T, S>& v) {
if (v.size() == 0) {
os << "empty map\n";
return os;
}
auto endit = v.end();
endit--;
os << "{";
for (auto it = v.begin(); it != v.end(); it++) {
os << "(" << (*it).first << " : " << (*it).second << ")";
if (it != endit) {
os << ", ";
}
}
os << "}";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<vector<T>>& v) {
for (auto& subv : v) {
for (auto& e : subv) {
os << e << " ";
}
os << "\n";
}
return os;
}
void gen_z(vector<int>& z, string s, int len) {
int l = 0, r = 0;
for (int i = 1; i < len; i++) {
if (i <= r) {
z[i] = min(r - i + 1, z[i - l]);
}
while (i + z[i] < len && s[z[i]] == s[i + z[i]]) {
z[i]++;
}
if (z[i] + i - 1 > r) {
l = i;
r = z[i] + i - 1;
}
}
}
void solve() {
string s;
cin >> s;
int len = s.length();
vector<int> z(len, 0);
multiset<int> sett;
vector<int> cv(len, 0);
gen_z(z, s, len);
for (int i = 1; i < len - 1; i++) {
int contr = z[i];
if (i + contr - 1 == len - 1) {
contr--;
}
if (contr) {
sett.insert(contr);
}
cv[i] = contr;
}
int ans_i = -1, ans_l = 0;
for (int i = len - 1; i >= 1; i--) {
if (cv[i]) {
sett.erase(sett.find(cv[i]));
}
if (!sett.size()) {
break;
}
if (*sett.rbegin() >= z[i] && i + z[i] - 1 == len - 1) {
if (z[i] > ans_l) {
ans_l = z[i];
ans_i = i;
}
}
}
if (ans_i != -1) {
for (int i = ans_i; i < ans_i + ans_l; i++) {
cout << s[i];
}
cout << "\n";
} else {
cout << "Just a legend";
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q = 1;
while (q-- > 0) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6 + 44;
const int mod = 1e8 + 9, b = 1413;
long long rcshnt[MN];
int n;
char a[MN];
bool check(int len) {
;
long long curr = 0;
for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;
long long pre = curr;
;
for (int i = len; i < n - 1; ++i) {
;
;
curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;
;
if ((pre - curr) % mod == 0) {
;
return true;
}
};
return false;
}
int main() {
scanf("%s", a);
n = strlen(a);
rcshnt[0] = 1;
for (int i = 1; i < MN; ++i) {
rcshnt[i] = rcshnt[i - 1] * b % mod;
}
for (int i = 0; i < 10; ++i)
;
vector<int> pre;
long long ph = 0, sh = 0;
for (int i = 0; i < n - 1; ++i) {
ph = (ph * b + a[i]) % mod;
sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;
if (ph == sh) {
pre.push_back(i + 1);
;
}
}
int low = 1, high = n - 1, ans = 0;
while (low <= high) {
int med = (low + high) / 2;
if (check(med)) {
ans = med;
low = med + 1;
} else
high = med - 1;
}
int res = -1;
for (auto c : pre) {
if (c <= ans) res = max(res, c);
}
if (res <= 0) {
printf("Just a legend\n");
} else {
a[res] = 0;
printf("%s\n", a);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 9;
const long long p = 31;
string s;
long long pref[1000005];
long long inv[1000005];
long long suff[1000005];
long long cal[1000005];
long long pw(long long a, long long b) {
if (b == 0)
return 1;
else if (b == 1)
return a % mod;
else {
long long x = pw(a, b / 2);
if (b % 2 == 0)
return (x * x) % mod;
else
return x * x % mod * a % mod;
}
}
long long subs(long long l, long long r) {
return (pref[r] - (l == 0 ? 0 : pref[l - 1]) + mod) % mod * inv[l] % mod;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
long long n = s.size();
for (long long i = 0; i < 1000005; ++i) {
if (i == 0)
cal[i] = 1;
else
cal[i] = (cal[i - 1] * p) % mod;
inv[i] = pw(cal[i], mod - 2);
}
for (long long i = 0; i < n; ++i) {
if (i == 0)
pref[i] = (s[i] - 'a' + 1) % mod;
else
pref[i] = (pref[i - 1] + (s[i] - 'a' + 1) * cal[i]) % mod;
}
for (long long j = n - 1; ~j; --j) {
suff[n - j - 1] = subs(j, n - 1);
}
vector<pair<long long, long long> > v;
for (long long i = 0; i < n - 1; ++i) {
if (pref[i] == suff[i]) v.push_back(pair<long long, long long>(pref[i], i));
}
if (v.empty()) {
cout << "Just a legend";
return 0;
}
long long ll = 0, mid, ans = -1;
long long rr = (long long)v.size() - 1;
bool ok = false, ex = false;
pair<long long, long long> tmp;
while (ll <= rr) {
mid = (rr + ll) / 2;
ok = false;
tmp = v[mid];
for (long long i = 1; i + tmp.second < n - 1; ++i) {
if (subs(i, i + tmp.second) == tmp.first) {
ll = mid;
ok = true;
ex = true;
break;
}
}
if (ok) {
ll = mid + 1;
if (ans < mid) ans = mid;
} else
rr = mid - 1;
}
if (!ex) {
for (long long i = 1; i + v[ans].second < n - 1; ++i) {
if (subs(i, i + v[ans].second) == v[ans].first) {
ex = true;
break;
}
}
}
if (ex) {
for (long long i = 0; i <= v[ans].second; ++i) cout << s[i];
} else
cout << "Just a legend";
}
|
#include <bits/stdc++.h>
char s[15000005];
int n, fail[15000005], num[15000005];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int j = 0;
for (int i = 1; i <= n; i++) {
for (; j && s[j + 1] != s[i]; j = fail[j])
;
if (s[j + 1] == s[i] && j + 1 != i) j++;
fail[i] = j;
}
for (int i = n; i >= 1; i--) {
++num[i];
num[fail[i]] += num[i];
}
while (n) {
if (num[fail[n]] > 2 && fail[n]) {
for (int i = 1; i <= fail[n]; i++) printf("%c", s[i]);
return 0;
}
n = fail[n];
}
printf("Just a legend\n");
}
|
#include <bits/stdc++.h>
using namespace std;
const long long PR = 19260817;
vector<int> constructKMP(string pattern) {
int n = pattern.size();
vector<int> next(n + 1, 0);
for (int i = 1, j = 0; i < n; i++) {
while (j > 0 && pattern[i] != pattern[j]) j = next[j - 1];
if (pattern[i] == pattern[j])
next[i] = ++j;
else
next[i] = j;
}
return next;
}
int main() {
string s;
cin >> s;
int n = s.size();
vector<int> v = constructKMP(s);
for (int i = n - 1; v[i]; i = v[i - 1]) {
for (int j = 0; j < n - 1; j++) {
if (v[i] == v[j]) {
cout << s.substr(0, v[j]);
return 0;
}
}
}
cout << "Just a legend\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int Next[maxn];
char mo[maxn];
int n2;
void GetNext() {
int i = 0, j = -1;
while (i < n2) {
if (j == -1 || mo[i] == mo[j]) {
++i, ++j, Next[i] = j;
} else
j = Next[j];
}
return;
}
int vis[maxn];
int main() {
scanf("%s", mo);
n2 = strlen(mo);
Next[0] = -1;
GetNext();
int tt = Next[n2];
while (tt != 0) {
vis[tt] = 1;
tt = Next[tt];
}
int ans = 0;
for (int i = 2; i < n2; i++) {
if (vis[Next[i]]) ans = max(ans, Next[i]);
}
if (ans > 0) {
for (int i = 0; i < ans; i++) printf("%c", mo[i]);
} else {
printf("Just a legend");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6 + 44;
const int mod = 1e9 + 9, b = 8899;
long long rcshnt[MN];
int n;
char a[MN];
bool check(int len) {
;
long long curr = 0;
for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;
long long pre = curr;
;
for (int i = len; i < n - 1; ++i) {
;
;
curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;
;
if ((pre - curr) % mod == 0) {
;
return true;
}
};
return false;
}
int main() {
scanf("%s", a);
n = strlen(a);
rcshnt[0] = 1;
for (int i = 1; i < MN; ++i) {
rcshnt[i] = rcshnt[i - 1] * b % mod;
}
for (int i = 0; i < 10; ++i)
;
vector<int> pre;
long long ph = 0, sh = 0;
for (int i = 0; i < n - 1; ++i) {
ph = (ph * b + a[i]) % mod;
sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;
if (ph == sh) {
pre.push_back(i + 1);
;
}
}
int low = 1, high = n - 1, ans = 0;
while (low <= high) {
int med = (low + high) / 2;
if (check(med)) {
ans = med;
low = med + 1;
} else
high = med - 1;
}
int res = -1;
for (auto c : pre) {
if (c <= ans) res = max(res, c);
}
if (res <= 0) {
printf("Just a legend\n");
} else {
a[res] = 0;
printf("%s\n", a);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2;
string s;
int z[N], freq[N], sum[N], l = -1, r = -1, ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> s;
int n = s.length();
for (int i = int(1); i <= int(n - 1); i++) {
if (i > r) {
while (z[i] + i < n && s[i + z[i]] == s[z[i]]) z[i]++;
if (z[i]) l = i, r = z[i] - 1 + i;
} else {
z[i] = min(z[i - l], r - i + 1);
while (z[i] + i < n && s[i + z[i]] == s[z[i]]) z[i]++;
if (z[i] + i - 1 > r) r = z[i] - 1 + i, l = i;
}
}
int ans = 0, mx = 0;
for (int i = int(1); i <= int(n); i++) {
if (z[i] == n - i && z[i] <= mx) {
ans = z[i];
break;
}
mx = max(mx, z[i]);
}
if (ans == 0)
cout << "Just a legend";
else
for (int i = int(0); i <= int(ans - 1); i++) cout << s[i];
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void zAlgo(string str, int z[]) {
int l = 0, r = 0;
z[0] = str.size();
for (int i = 1; i < str.size(); i++) {
if (i > r) {
int n = 0;
while (n + i < str.size() && str[n] == str[n + i]) {
n++;
}
z[i] = n;
if (n) {
l = i;
r = i + n - 1;
}
} else {
int p = i - l;
if (z[p] < r - i + 1) {
z[i] = z[p];
} else {
int k = r + 1;
while (k < str.size() && str[k] == str[k - i]) {
k++;
}
z[i] = k - i;
l = i;
r = k - 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string str;
cin >> str;
int z[str.size()];
zAlgo(str, z);
int maxz = -1;
int i = 1;
for (; i < str.size(); i++) {
if (z[i] == str.size() - i && maxz >= z[i]) {
cout << str.substr(0, z[i]) << '\n';
break;
}
maxz = max(maxz, z[i]);
}
if (i == str.size()) {
cout << "Just a legend\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 123;
const int mod = 1e9 + 8;
const int inf = 1e9 + 123;
const long long INF = 1e18 + 123;
const double eps = (double)(1e-9);
const double pi = 3.14159265358979323846264;
const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const pair<int, int> dir[] = {make_pair(-1, 0), make_pair(0, -1),
make_pair(1, 0), make_pair(0, 1)};
const pair<int, int> dirr[] = {
make_pair(-2, 1), make_pair(1, -2), make_pair(2, -1), make_pair(-1, 2),
make_pair(2, 1), make_pair(1, 2), make_pair(-2, -1), make_pair(-1, -2)};
string second;
long long p[N], h[N], pr = 31;
string ans;
long long get(long long i, long long j) {
return (h[j] - (h[i] * p[j - i]) % mod + mod) % mod;
}
bool check(long long len) {
long long i = len;
long long j = second.size() - len + 1;
long long x = h[i];
long long y = get(j - 1, second.size());
if (x != y) {
return 0;
}
bool ok = 0;
for (long long k = len + 1; k < second.size(); ++k) {
long long z = get(k - len, k);
if (x == z && y == z) {
ok = 1;
break;
}
}
if (!ok) {
return 0;
}
ans = "";
for (long long i = 1; i <= len; ++i) {
ans += second[i - 1];
}
return 1;
}
int main() {
cin.tie(0);
cout.tie(0);
cout.precision(0);
ios_base::sync_with_stdio(0);
cin >> second;
p[0] = 1;
for (long long i = 1; i <= 1e6; ++i) {
p[i] = (p[i - 1] * pr) % mod;
}
for (long long i = 1; i <= second.size(); ++i) {
h[i] = (h[i - 1] * pr + second[i - 1]) % mod;
}
for (long long i = second.size(); i > 0; --i) {
if (check(i)) {
break;
}
}
if (!ans.size()) {
cout << "Just a legend";
return 0;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 1e9 + 9;
int main() {
int t;
t = 1;
while (t--) {
string s;
cin >> s;
int l = s.length();
s = '1' + s;
vector<long long> p_pow(l);
p_pow[0] = 1;
for (int i = 1; i < (int)p_pow.size(); i++) {
p_pow[i] = (p_pow[i - 1] * p) % m;
}
vector<long long> hash(l + 1);
for (int i = 1; i <= l; i++) {
hash[i] = (hash[i - 1] + (s[i] - 'a' + 1) * p_pow[i - 1]) % m;
}
vector<int> lens;
vector<long long> comps;
for (int i = 1; i <= l - 1; i++) {
int lft = i;
int rt = l - i + 1;
long long h1 = hash[i];
long long h2 = (hash[l] - hash[rt - 1] + m) % m;
if (h1 * p_pow[rt - 1] % m == h2) {
lens.push_back(i);
comps.push_back(h1);
}
}
int le = 0;
int r = lens.size() - 1;
int ans = -1;
vector<int> inds;
for (int i = 1; i <= l; i++) {
if (i != 1 && s[i] == s[1]) {
inds.push_back(i);
}
}
if (inds.size() > 0) inds.pop_back();
while (le <= r && inds.size() > 0) {
int mid = (le + r) / 2;
int cand_len = lens[mid];
bool found = false;
for (auto &val : inds) {
int lft = val;
int rt = val + cand_len - 1;
if (rt >= l) continue;
long long h = (hash[rt] - hash[lft - 1] + m) % m;
if ((comps[mid] * p_pow[lft - 1]) % m == h) {
found = true;
break;
}
}
if (found) {
ans = cand_len;
le = mid + 1;
} else
r = mid - 1;
}
if (ans != -1) {
for (int i = 1; i <= ans; i++) cout << s[i];
} else
cout << "Just a legend";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<int, int> mapp;
string s;
int Z[1000001];
void Zval() {
int left = 0, right = 0;
for (int k = 1; k < s.size(); k++) {
if (k > right) {
left = right = k;
while (right < s.size() && s[right] == s[right - left]) right++;
Z[k] = right - left;
right--;
} else {
int k1 = k - left;
if (Z[k1] < right - k + 1)
Z[k] = Z[k1];
else {
left = k;
while (right < s.size() && s[right] == s[right - left]) right++;
Z[k] = right - left;
right--;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
int n, m;
int i, j;
cin >> s;
n = s.size();
Zval();
int maxz = -1, ans = -1;
for (i = 0; i < n; i++) {
if (Z[i] == n - i && maxz >= n - i) {
ans = i;
break;
}
maxz = max(maxz, Z[i]);
}
if (ans == -1)
cout << "Just a legend";
else
cout << s.substr(ans, n);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int n = s.length();
int j = 0, i = 1;
int prefix[n];
prefix[0] = 0;
int k = 0;
while (i < n) {
if (s[i] == s[j]) {
prefix[i] = j + 1;
i++;
j++;
} else {
if (j != 0)
j = prefix[j - 1];
else
prefix[i] = 0, i++;
}
}
j = prefix[n - 1];
for (int i = 0; i < n - 1; i++) k = max(k, prefix[i]);
while (j > 0 && j > k) j = prefix[j - 1];
if (j)
cout << s.substr(0, j) << endl;
else
cout << "Just a legend\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
};
string s;
cin >> s;
int n = s.size();
vector<int> p(n);
p[0] = 0;
for (int i = 1; i < n; i++) {
int a = p[i - 1];
while (a > 0 && s[i] != s[a]) {
a = p[a - 1];
}
if (s[a] == s[i]) a++;
p[i] = a;
}
if (p[n - 1] == 0)
cout << "Just a legend" << endl;
else {
int boo = 0;
for (int i = 0; i < n - 1; i++) {
if (p[i] == p[n - 1]) boo = 1;
}
if (boo)
cout << s.substr(0, p[n - 1]) << endl;
else {
if (p[p[n - 1] - 1] == 0)
cout << "Just a legend" << endl;
else {
cout << s.substr(0, p[p[n - 1] - 1]) << endl;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6 + 44;
const int mod = 1e9 + 9, b = 8889;
long long rcshnt[MN];
int n;
char a[MN];
bool check(int len) {
;
long long curr = 0;
for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;
long long pre = curr;
;
for (int i = len; i < n - 1; ++i) {
;
;
curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;
;
if ((pre - curr) % mod == 0) {
;
return true;
}
};
return false;
}
int main() {
scanf("%s", a);
n = strlen(a);
rcshnt[0] = 1;
for (int i = 1; i < MN; ++i) {
rcshnt[i] = rcshnt[i - 1] * b % mod;
}
for (int i = 0; i < 10; ++i)
;
vector<int> pre;
long long ph = 0, sh = 0;
for (int i = 0; i < n - 1; ++i) {
ph = (ph * b + a[i]) % mod;
sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;
if (ph == sh) {
pre.push_back(i + 1);
;
}
}
int low = 1, high = n - 1, ans = 0;
while (low <= high) {
int med = (low + high) / 2;
if (check(med)) {
ans = med;
low = med + 1;
} else
high = med - 1;
}
int res = -1;
for (auto c : pre) {
if (c <= ans) res = max(res, c);
}
if (res <= 0) {
printf("Just a legend\n");
} else {
a[res] = 0;
printf("%s\n", a);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int nex[N];
char a[N];
bool v[N];
inline int re() {
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar()) p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return p ? -x : x;
}
int main() {
int i, j, k, x, y, n, m;
scanf("%s", a);
n = strlen(a);
for (i = 1, j = 0; i < n; i++) {
for (; j && a[i] != a[j]; j = nex[j - 1])
;
if (a[i] == a[j]) ++j;
nex[i] = j;
}
if (!nex[n - 1]) return printf("Just a legend"), 0;
for (i = 1; i < n - 1; i++) v[nex[i]] = 1;
for (j = nex[n - 1]; j; j = nex[j - 1])
if (v[j]) {
for (i = 0; i < j; i++) printf("%c", a[i]);
return 0;
}
printf("Just a legend");
return 0;
}
|
#include <bits/stdc++.h>
unsigned h[1024000], t;
char s[1024000];
int L, M, i, l;
unsigned p(int x) {
unsigned r = 1, t = 29;
for (; x; x >>= 1, t *= t)
if (x & 1) r *= t;
return r;
}
int main() {
gets(s);
for (t; s[l]; ++l) h[l] = t = t * 29 + s[l];
for (M = l - 1; M && !L; --M) {
t = p(M);
if (h[l - 1] != h[l - 1 - M] * t + h[M - 1]) continue;
for (i = 0; i + M + 1 < l; ++i)
if (h[i] * t + h[M - 1] == h[i + M]) L = M;
}
s[L] = 0;
puts(L ? s : "Just a legend");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int z[1000006] = {0}, h[1000005] = {0};
int main() {
string s;
cin >> s;
int l, r, ma = -1, i;
l = r = 0;
for (i = 1; i < s.size(); i++) {
if (i > r) {
l = r = i;
while (s[r - l] == s[r]) r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (z[k] < r - i + 1)
z[i] = z[k];
else {
l = i;
while (s[r - l] == s[r]) r++;
z[i] = r - l;
r--;
}
}
}
for (i = 1; i < s.size(); i++)
if (i + z[i] == s.size()) h[z[i]] = z[i];
;
for (i = 1; i < 1000001; i++)
if (!h[i]) h[i] = h[i - 1];
for (i = 1; i < s.size(); i++)
if (z[i])
if (z[i] + i == s.size())
ma = max(ma, h[z[i] - 1]);
else
ma = max(ma, h[z[i]]);
if (ma > 0)
for (i = 0; i < ma; i++) printf("%c", s[i]);
else
printf("Just a legend");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int p = 31;
vector<long long> p_pow;
vector<long long> h;
long long get_h(int i1, int len) {
long long h1 = h[i1 + len - 1];
if (i1) h1 -= h[i1 - 1];
return h1;
}
string s;
bool check(int n) {
if (get_h(0, n) * p_pow[s.length() - 1 - n] != get_h(s.length() - n, n))
return false;
for (int i = 1; i < s.length() - n; i++)
if (get_h(i, n) == get_h(0, n) * p_pow[i - 1]) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
p_pow.resize(s.length() + 1);
h.resize(s.length());
p_pow[0] = p;
for (int i = 1; i < s.length(); i++) p_pow[i] = p * p_pow[i - 1];
for (int i = 0; i < s.length(); i++) {
h[i] = (s[i] - 'a' + 1) * p_pow[i];
if (i) h[i] += h[i - 1];
}
for (int i = s.length() - 2; i > 0; i--) {
if (check(i)) {
string res = "";
for (int j = 0; j < i; j++) res += s[j];
cout << res;
return 0;
}
}
cout << "Just a legend";
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxC = 2147483647;
const int maxNode = 10230997;
const int maxN = 1000000;
string str;
int n, z[maxN];
void buildZ(string& str) {
z[0] = 0;
int l = 0;
for (int i = 1; i < (int)str.size(); ++i) {
while (l && str[i] != str[l]) l = z[l - 1];
if (str[i] == str[l]) ++l;
z[i] = l;
}
}
int main() {
cin >> str;
buildZ(str);
int res = z[str.size() - 1];
bool kt = 0;
for (int i = 0; i < (int)str.size() - 1; ++i) {
if (z[i] == res) {
kt = 1;
break;
}
}
if (!kt) {
res = z[res - 1];
}
if (res == 0)
cout << "Just a legend";
else
cout << str.substr(0, res);
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
long double PI = 3.14159265358979323846;
const long long modo = 998244353;
const long long inf = 1e16;
const long long ms = (1e6) + 5;
char ar[ms];
int tt[ms] = {0};
int ss[ms] = {0};
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
string s;
cin >> s;
int n = s.length();
int i;
int pr = 0;
for (i = 1; i < n; i++) {
while (pr > 0 && s[pr] != s[i]) {
pr = tt[pr - 1];
}
if (s[i] == s[pr]) {
pr++;
}
tt[i] = pr;
}
ss[0] = 1;
for (i = 1; i < (n - 1); i++) {
++ss[tt[i]];
}
pr = tt[n - 1];
while (!ss[pr]) {
pr = tt[pr - 1];
}
if (!pr) {
cout << "Just a legend";
return 0;
}
for (i = 0; i < pr; i++) cout << s[i];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool vis[2 * 1000996];
int idx, len;
struct State {
int len, link;
map<char, int> nxtc;
} SA[2 * 1000996];
int NumOcc[2 * 1000996];
char str[1000996];
vector<pair<int, int> > lenState;
class SuffixAutomata {
int sz, last, fnl;
void init(int idx) {
SA[idx].len = 0, SA[idx].link = -1;
SA[idx].nxtc.clear();
}
void traverse(int cur, char *ch, int l) {
while (*ch && NumOcc[cur] >= 3 && cur < sz) {
if (vis[cur]) len = l;
int p = SA[cur].nxtc[*ch];
cur = p, ++ch, ++l;
}
}
public:
SuffixAutomata() {
sz = last = 0;
init(0);
lenState.clear();
++sz;
}
int size() { return sz; }
void extend(char ch) {
int p, q, clone, cur = sz++;
init(cur);
fnl = cur;
SA[cur].len = SA[last].len + 1;
NumOcc[cur] = 1;
lenState.push_back(make_pair(SA[cur].len, cur));
for (p = last; p != -1 && SA[p].nxtc.count(ch) == 0; p = SA[p].link)
SA[p].nxtc[ch] = cur;
if (p == -1)
SA[cur].link = 0;
else {
q = SA[p].nxtc[ch];
if (SA[p].len + 1 == SA[q].len)
SA[cur].link = q;
else {
clone = sz++;
init(clone);
SA[clone] = SA[q];
SA[clone].len = SA[p].len + 1;
for (; p != -1 && SA[p].nxtc[ch] == q; p = SA[p].link)
SA[p].nxtc[ch] = clone;
SA[cur].link = SA[q].link = clone;
NumOcc[clone] = 0;
lenState.push_back(make_pair(SA[clone].len, clone));
}
}
last = cur;
}
void traverse(char *str) { traverse(0, str, 0); }
void numOcc() {
int i, p, q;
sort(lenState.begin(), lenState.end());
for (i = lenState.size() - 1; i >= 0; --i) {
q = lenState[i].second;
p = SA[q].link;
NumOcc[p] += NumOcc[q];
}
}
void crawl() {
int cur = fnl;
memset(vis, false, sizeof(vis));
while (cur > 0) {
vis[cur] = true;
cur = SA[cur].link;
}
}
};
int main() {
int i;
while (scanf("%s", str) != EOF) {
SuffixAutomata sa;
for (i = 0; str[i]; ++i) sa.extend(str[i]);
sa.numOcc();
sa.crawl();
len = -1;
sa.traverse(str);
if (len <= 0)
puts("Just a legend");
else {
str[len] = 0;
puts(str);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt[3000000];
vector<int> p_fun(string s) {
int n = s.length();
vector<int> pi(n);
for (int i = 1; i < n; ++i) {
int j = pi[i - 1];
while (s[i] != s[j] && j > 0) j = pi[j - 1];
if (s[i] == s[j]) j++;
pi[i] = j++;
cnt[j]++;
}
return pi;
}
int main() {
cin >> s;
vector<int> pi = p_fun(s);
int n = s.length() - 1;
int len = pi[n];
if (len == 0) {
cout << "Just a legend";
return 0;
}
for (int i = 0; i < n - 1; ++i)
if (pi[i] == len) {
cout << s.substr(0, len);
return 0;
}
len = pi[len - 1];
if (len == 0) {
cout << "Just a legend";
return 0;
}
cout << s.substr(0, len);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> z_foo(string s) {
int n = s.length(), i, l = 0, r = 0;
vector<int> z(n);
z[0] = n;
for (i = 1; i < n; i++) {
if (i <= r) {
z[i] = min(z[i - l], r - i + 1);
}
while (z[i] + i < n && s[z[i] + i] == s[z[i]]) ++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
int main() {
string s;
cin >> s;
vector<int> z = z_foo(s);
int maxi = 0;
int n = s.length();
vector<int> m(n + 1);
for (int i = n - 1; i >= 0; i--) m[i] = max(m[i + 1], z[i]);
int x;
for (int i = 1; i < n; i++) {
if (z[i]) {
if (z[i] + i == n) {
x = z[i] - 1;
x = min(m[n - x], x);
if (z[n - x] != x) x = 0;
maxi = max(x, maxi);
} else {
x = z[i];
x = min(m[n - x], x);
if (z[n - x] != x) x = 0;
maxi = max(x, maxi);
}
}
}
if (maxi == 0) {
cout << "Just a legend";
} else {
cout << s.substr(0, maxi);
}
}
|
#include <bits/stdc++.h>
using namespace std;
void Z_algorithm(string& s, vector<int>& Z) {
Z.assign(s.length(), -1);
int L = 0, R = 0, n = s.length();
for (int i = 1; i < n; ++i) {
if (i > R) {
L = R = i;
while (R < n && s[R - L] == s[R]) R++;
Z[i] = R - L;
R--;
} else if (Z[i - L] >= R - i + 1) {
L = i;
while (R < n && s[R - L] == s[R]) R++;
Z[i] = R - L;
R--;
} else
Z[i] = Z[i - L];
}
Z[0] = n;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string str;
cin >> str;
vector<int> Z;
Z_algorithm(str, Z);
int z_max = 0, l = 0, n = str.length();
for (int i = 1; i < n; ++i) {
if (Z[i] == n - i && z_max >= n - i) {
l = n - i;
break;
}
z_max = max(z_max, Z[i]);
}
if (l == 0)
cout << "Just a legend" << endl;
else
for (int i = 0; i < l; ++i) cout << str[i];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char T[1000005], s[1000005];
int f[1000005];
void getfail(char *p, int len) {
int j;
f[0] = j = -1;
for (int i = 1; i < len; i++) {
while (j >= 0 && p[j + 1] != p[i]) j = f[j];
if (p[j + 1] == p[i]) j++;
f[i] = j;
}
}
int find(char *s, int len) {
int x = strlen(T);
int j = -1;
for (int i = 0; i < x; i++) {
while (j >= 0 && s[j + 1] != T[i]) j = f[j];
if (s[j + 1] == T[i]) j++;
if (j + 1 == len) return len;
}
return -1;
}
int main() {
int pp = -1;
scanf("%s", s);
if (strlen(s) <= 2)
printf("Just a legend\n");
else {
getfail(s, strlen(s));
strncpy(T, s + 1, strlen(s) - 2);
T[strlen(s) - 2] = '\0';
int j = strlen(s) - 1;
while (f[j] >= 0) {
if (f[j] + 1 > pp) {
int t = find(s, f[j] + 1);
if (t > pp) pp = t;
}
j = f[j];
}
if (pp != -1)
for (int i = 0; i < pp; i++) printf("%c", s[i]);
else
printf("Just a legend\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6 + 44;
const int mod = 1e9 + 9, b = 12883;
long long rcshnt[MN];
int n;
char a[MN];
bool check(int len) {
;
long long curr = 0;
for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;
long long pre = curr;
;
for (int i = len; i < n - 1; ++i) {
;
;
curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;
;
if ((pre - curr) % mod == 0) {
;
return true;
}
};
return false;
}
int main() {
scanf("%s", a);
n = strlen(a);
rcshnt[0] = 1;
for (int i = 1; i < MN; ++i) {
rcshnt[i] = rcshnt[i - 1] * b % mod;
}
for (int i = 0; i < 10; ++i)
;
vector<int> pre;
long long ph = 0, sh = 0;
for (int i = 0; i < n - 1; ++i) {
ph = (ph * b + a[i]) % mod;
sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;
if (ph == sh) {
pre.push_back(i + 1);
;
}
}
int low = 1, high = n - 1, ans = 0;
while (low <= high) {
int med = (low + high) / 2;
if (check(med)) {
ans = med;
low = med + 1;
} else
high = med - 1;
}
int res = -1;
for (auto c : pre) {
if (c <= ans) res = max(res, c);
}
if (res <= 0) {
printf("Just a legend\n");
} else {
a[res] = 0;
printf("%s\n", a);
}
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> vec[(long long)(1e6)];
vector<long long> comp;
long long vis[(long long)(1e6)];
long long dis[(long long)(1e6)];
void DFS(long long x) {
vis[x] = true;
for (long long i = 0; i < vec[x].size(); i++) {
if (!vis[vec[x][i]]) {
DFS(vec[x][i]);
}
}
}
long long power(long long x, long long y) {
long long temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp * temp) % ((long long)(1e9 + 7));
else
return (x * temp * temp) % (long long)(1e9 + 7);
}
const long long N = 100000;
long long pr[1005];
void coding() {
long long i, j, k, l, n, m, t;
string s;
cin >> s;
n = s.length();
vector<long long> pref(n);
map<long long, bool> mp;
for (long long i = 1; i < n; i++) {
j = pref[i - 1];
while (j > 0 && s[j] != s[i]) j = pref[j - 1];
if (s[i] == s[j]) j++;
pref[i] = j;
if (i != n - 1) mp[pref[i]] = true;
}
j = pref[n - 1];
while (j > 0) {
if (mp[j]) {
for (long long i = 0; i < j; i++) cout << s[i];
return;
}
j = pref[j - 1];
}
cout << "Just a legend";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cout << fixed << setprecision(11);
while (t--) {
coding();
}
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string s;
vector<long long int> zval;
long long int n;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
n = s.length();
zval.resize(n, 0);
long long int L = 0, R = 0;
for (long long int i = 1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && s[R - L] == s[R]) R++;
zval[i] = R - L;
R--;
} else {
long long int k = i - L;
if (zval[k] < R - i + 1)
zval[i] = zval[k];
else {
L = i;
while (R < n && s[R - L] == s[R]) R++;
zval[i] = R - L;
R--;
}
}
}
vector<long long int> maxi;
maxi.resize(n, 0);
for (long long int i = 1; i < n; i++) {
maxi[i] = max(maxi[i - 1], zval[i]);
}
long long int suff = -1;
for (long long int i = (n - 1); i >= 1; i--) {
if ((zval[i] + i) == n && maxi[i - 1] >= zval[i]) {
suff = i;
}
}
if (suff == -1) {
cout << "Just a legend"
<< "\n";
return 0;
}
long long int len = n - suff;
long long int pre = -1;
for (long long int i = 1; i < suff; i++) {
if (zval[i] >= len) {
pre = i;
break;
}
}
if (pre == -1) {
cout << "Just a legend"
<< "\n";
return 0;
}
for (long long int i = 0; i < len; i++) {
cout << s[i];
}
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int z[1000010];
vector<int> vec;
string s;
map<int, int> mp;
void zfunction() {
int n = (int)s.length();
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
}
int main() {
int mnhna = -1, maxx = -100000;
cin >> s;
zfunction();
for (int i = 1; i < s.size(); i++) {
++mp[z[i]];
maxx = max(maxx, z[i]);
}
for (int i = 1; i < s.size(); i++) {
if (i + z[i] >= s.size() && ((mp[z[i]] > 1) || maxx > z[i])) {
mnhna = i;
break;
}
}
if (mnhna == -1) {
cout << "Just a legend" << endl;
} else
cout << s.substr(mnhna, s.size() - mnhna) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int mod = 10000007;
long long pwr(long long a, long long b, long long mod) {
if (b == 0) return 1;
long long temp = pwr(a, b / 2, mod);
temp = (temp * temp) % mod;
if (b & 1) temp = (temp * a) % mod;
return temp;
}
long long pwr(long long a, long long b) {
if (b == 0) return 1;
long long temp = pwr(a, b / 2);
temp = (temp * temp);
if (b & 1) temp = (temp * a);
return temp;
}
bool *isPrime;
void generatePrimeSieve(const int lim) {
isPrime = (bool *)malloc(lim + 1);
memset(isPrime, true, lim + 1);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i <= lim; ++i)
if (isPrime[i])
for (int j = i + i; j <= lim; j += i) isPrime[j] = false;
}
vector<vector<int> > identityMatrix;
vector<vector<int> > mul(const vector<vector<int> > &a,
const vector<vector<int> > &b) {
int n = a.size();
vector<vector<int> > ans(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
ans[i][j] += ((long long)a[i][k] * b[k][j]) % mod;
ans[i][j] %= mod;
}
}
}
return ans;
}
vector<vector<int> > pwr(const vector<vector<int> > &a, long long n) {
if (n == 0) {
assert(false);
return identityMatrix;
}
if (n == 1) return a;
vector<vector<int> > tmp = pwr(a, n / 2);
tmp = mul(tmp, tmp);
if (n & 1) tmp = mul(a, tmp);
return tmp;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long modularInverse(long long a, long long m) { return pwr(a, m - 2, m); }
void kmp_failure(string &s, vector<int> &f) {
f.clear();
int n = s.size();
f.resize(n, 0);
int curr;
for (int i = 1; i < n; ++i) {
curr = f[i - 1];
while (curr > 0 && s[i] != s[curr]) {
curr = f[curr - 1];
}
if (s[i] == s[curr])
f[i] = curr + 1;
else
f[i] = 0;
}
}
int cnt[1000005];
vector<int> fail;
int main() {
string s;
cin >> s;
int n = s.size();
kmp_failure(s, fail);
for (int i = 1; i < n - 1; ++i) {
cnt[fail[i]] = 1;
}
int curr = fail[n - 1];
while (curr) {
if (cnt[curr]) {
cout << s.substr(0, curr);
return 0;
}
curr = fail[curr - 1];
}
cout << "Just a legend\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int pre[1000001];
void Prefix(int n, string str) {
int i, j;
pre[0] = 0;
i = 1;
j = 0;
while (i < n) {
if (str[i] == str[j]) {
pre[i] = j + 1;
i++;
j++;
} else {
if (j == 0) {
pre[i] = 0;
i++;
} else {
j = pre[j - 1];
}
}
}
}
int main() {
int i, j, n, m, k, index;
string str;
cin >> str;
n = str.length();
Prefix(n, str);
if (pre[n - 1] == 0) {
cout << "Just a legend" << endl;
return 0;
} else {
j = pre[n - 1];
for (i = 0; i < n - 1; i++) {
if (pre[i] == j) {
cout << str.substr(0, j) << endl;
return 0;
}
}
j = pre[pre[n - 1] - 1];
if (j == 0) {
cout << "Just a legend" << endl;
return 0;
} else {
cout << str.substr(0, j) << endl;
return 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 1e9 + 9;
long long p_pow[1000009];
void preprocess() {
p_pow[0] = 1;
for (int i = 1; i < 1000009; i++) p_pow[i] = (p_pow[i - 1] * p) % m;
}
long long get_hash(string const& s) {
long long hash = 0;
for (int i = 0; i < s.size(); i++)
hash = (hash + (s[i] - 'a' + 1) * p_pow[i]) % m;
return hash;
}
vector<long long> get_prefix_hash(string const& s) {
int siz = s.size();
vector<long long> h(siz + 1, 0);
for (int i = 0; i < siz; i++)
h[i + 1] = (h[i] + (s[i] - 'a' + 1) * p_pow[i]) % m;
return h;
}
long long get_sub(int start, int siz, const vector<long long>& h) {
return (h[start + siz] - h[start] + m) % m;
}
void solve() {
preprocess();
string s;
cin >> s;
int n = s.size();
vector<int> sizs;
vector<long long> pres = get_prefix_hash(s);
for (int i = 1; i <= n - 2; i++) {
if (get_sub(0, i, pres) * p_pow[n - i] % m == get_sub(n - i, i, pres))
sizs.push_back(i);
}
int l = 0;
int r = sizs.size();
while (l < r) {
int mm = (l + r) / 2;
int len = sizs[mm];
int value = get_sub(0, len, pres);
int ok = 0;
for (int i = 1; i + len - 1 < n - 1; i++)
if (get_sub(i, len, pres) == value * p_pow[i] % m) ok = 1;
if (ok)
l = mm + 1;
else
r = mm;
}
l--;
if (l >= 0)
cout << s.substr(0, sizs[l]) << endl;
else
cout << "Just a legend" << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> z_function(const string& s) {
int n = s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) {
z[i] = min(r - i + 1, z[i - l]);
}
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
z[i]++;
}
if (i + z[i] - 1 > r) {
l = i, r = i + z[i] - 1;
}
}
return z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int n = s.size();
vector<int> z = z_function(s);
int p = 0;
vector<int> mx(n);
for (int i = 1; i < n - 1; i++) {
mx[i] = max(mx[i - 1], z[i]);
}
for (int i = n - 1; i > 0; i--) {
if (i + z[i] == n && mx[i - 1] >= z[i]) {
p = i;
}
}
if (p) {
cout << s.substr(p) << '\n';
} else {
cout << "Just a legend\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 80000000;
const int MOD = 998244353;
const int N = 1000001;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
vector<int> z(string s) {
int n = (int)s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
int main() {
{
ios::sync_with_stdio(0);
cin.tie(NULL);
};
string s;
cin >> s;
int n = s.size();
vector<int> a = z(s);
int maxi = 0;
int res = 0;
for (int i = 0; i < int(n); i++) {
if (a[i] + i == n && maxi >= a[i]) {
res = a[i];
break;
}
maxi = max(maxi, a[i]);
}
if (res == 0) {
cout << "Just a legend"
<< "\n";
} else
cout << s.substr(0, res) << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
string str;
int pre[1000009];
bool flag[1000009];
int KMP() {
memset(flag, 0, sizeof(flag));
int k = 0;
pre[0] = 0;
for (int i = 1; i < ((int)str.size()); i++) {
while (k > 0 && str[k] != str[i]) k = pre[k - 1];
if (str[i] == str[k]) k++;
pre[i] = k;
}
int val = pre[((int)str.size()) - 1];
while (val > 0) {
flag[val] = 1;
val = pre[val - 1];
}
int max_val = -1;
for (int i = 1; i < ((int)str.size()); i++) {
val = pre[i];
if (flag[val]) {
if (0 < (i + 1) - val && i < ((int)str.size()) - 1) {
max_val = max(val, max_val);
}
}
}
return max_val;
}
int main() {
while (cin >> str) {
int ans = KMP();
if (ans != -1)
cout << str.substr(0, ans) << endl;
else
cout << "Just a legend" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int break_point() {
char c;
while ((c = getchar()) != '\n')
;
return 0;
}
template <typename T>
void read_integer(T &r) {
bool sign = 0;
r = 0;
char c;
while (1) {
c = getchar();
if (c == '-') {
sign = 1;
break;
}
if (c != ' ' && c != '\n') {
r = c - '0';
break;
}
}
while (1) {
c = getchar();
if (c == ' ' || c == '\n') break;
r = r * 10 + (c - '0');
}
if (sign) r = -r;
}
long long binpow(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a;
a *= a;
b >>= 1;
}
return ret;
}
long long binpow(long long a, long long b, int mod) {
long long ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ret;
}
inline int getbit(int x, int b) { return (x >> b) & 1; }
inline int setbit(int x, int b) { return x | (1 << b); }
inline void _setbit(int &x, int b) { x = setbit(x, b); }
inline long long setbit(long long x, int b) { return x | (1ll << b); }
inline void _setbit(long long &x, int b) { x = setbit(x, b); }
inline int unsetbit(int x, int b) { return x & (INT_MAX - (1 << b)); }
inline void _unsetbit(int &x, int b) { x = unsetbit(x, b); }
inline int countbit(int x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline long long countbit(long long x) {
return countbit(int(x & INT_MAX)) + countbit(int(x >> 32) & INT_MAX);
}
inline void printbit(int x, int len) {
for (int i = len - 1; i >= 0; i--) printf("%d", getbit(x, i));
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
template <typename A, typename B>
ostream &operator<<(ostream &stream, const pair<A, B> &p) {
stream << "{" << p.first << "," << p.second << "}";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const vector<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A, typename B>
ostream &operator<<(ostream &stream, const map<A, B> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const set<A> &v) {
stream << "[";
for (auto itr = v.begin(); itr != v.end(); itr++) stream << *itr << " ";
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const stack<A> &v) {
stack<A> st = v;
stream << "[";
while (!st.empty()) {
stream << st.top() << " ";
st.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const priority_queue<A> &v) {
priority_queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.top() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const queue<A> &v) {
queue<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop();
}
stream << "]";
return stream;
}
template <typename A>
ostream &operator<<(ostream &stream, const deque<A> &v) {
deque<A> q = v;
stream << "[";
while (!q.empty()) {
stream << q.front() << " ";
q.pop_front();
}
stream << "]";
return stream;
}
void run();
int main() {
srand(time(NULL));
run();
return 0;
}
const int mod = 0;
const int N = 1000006;
namespace {
char s[N];
int p[N];
} // namespace
void run() {
scanf("%s", s);
int n = strlen(s);
for (int i = 1; i < n; ++i) {
int j = p[i - 1];
while (j > 0 && s[i] != s[j]) j = p[j - 1];
if (s[i] == s[j]) ++j;
p[i] = j;
}
if (p[n - 1] == 0) {
puts("Just a legend");
return;
};
;
;
;
for (int i = 1; i < n - 1; ++i) {
if (p[i] == p[n - 1]) {
for (int j = 0; j < p[n - 1]; ++j) putchar(s[j]);
putchar('\n');
return;
}
}
if (p[p[n - 1] - 1] == 0)
puts("Just a legend");
else {
for (int i = 0; i < p[p[n - 1] - 1]; ++i) putchar(s[i]);
putchar('\n');
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 9;
bool mark[MAX];
int f[MAX], k, n;
int main() {
string s;
cin >> s, n = s.size();
for (int i = 1; i < n; i++) {
while (k && s[i] != s[k]) k = f[k - 1];
if (s[i] == s[k]) k++;
if (i < n - 1) mark[k] = true;
f[i] = k;
}
mark[0] = true, k = f[n - 1];
while (k && !mark[k]) k = f[k - 1];
if (k)
cout << s.substr(0, k);
else
cout << "Just a legend";
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int z[1000005];
int cnt[1000005];
void update(int id) {
if (id == 0) return;
while (id < 1000005) {
cnt[id]++;
id += id & -id;
}
}
void z_algo() {
int n = s.size();
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (r >= i) z[i] = min(z[i - l], r - i + 1);
while (s[i + z[i]] == s[z[i]]) z[i]++;
if (i + z[i] - 1 > r) {
r = i + z[i] - 1;
l = i;
}
update(z[i]);
}
}
int sum(int id) {
int s = 0;
while (id) {
s += cnt[id];
id -= id & -id;
}
return s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s;
z_algo();
int n = s.size();
z[0] = n;
update(z[n]);
vector<pair<int, int> > ans;
for (int l = n; l >= 1; l--) {
if (z[n - l] == l && sum(1000005 - 1) - sum(l - 1) + 1 >= 3) {
for (int i = 0; i < l; i++) cout << s[i];
return 0;
}
}
cout << "Just a legend";
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
string s;
int n, L, R, z[maxn], mx;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> s;
n = s.size();
for (int i = 1; i < n; i++)
if (i > R) {
L = R = i;
while (R < n && s[R] == s[R - L]) R++;
z[i] = R - L;
R--;
} else if (z[i - L] > R - i) {
L = i;
while (R < n && s[R] == s[R - L]) R++;
z[i] = R - L;
R--;
} else
z[i] = z[i - L];
for (int i = 1; i < n; i++) {
if (z[i] == n - i && mx >= z[i]) {
for (int ind = 0; ind < z[i]; ind++) cout << s[ind];
cout << "\n";
return 0;
}
mx = max(mx, z[i]);
}
cout << "Just a legend\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> prefix_function(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j]) j = pi[j - 1];
if (s[i] == s[j]) j++;
pi[i] = j;
}
return pi;
}
void solve() {
string s;
cin >> s;
vector<int> p = prefix_function(s);
long long int n = s.length();
if (n <= 2) {
cout << "Just a legend" << endl;
return;
}
if (p[n - 1] == 0) {
cout << "Just a legend" << endl;
return;
}
long long int ans = p[n - 1];
bool found = false;
for (long long int i = 1; i < n - 1; i++) {
if (p[i] == p[n - 1]) {
found = true;
break;
}
}
if (found) {
for (long long int i = 0; i < ans; i++) {
cout << s[i];
}
cout << endl;
} else {
if (p[p[n - 1] - 1] == 0) {
cout << "Just a legend" << endl;
return;
} else {
ans = p[p[n - 1] - 1];
for (long long int i = 0; i < ans; i++) {
cout << s[i];
}
cout << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
static bool cmp(pair<int, int> p1, pair<int, int> p2) {
if (p1.first == p2.first) return p1.second < p2.second;
return p1.first > p2.first;
}
vector<int> getZ(string s, int n) {
vector<int> z(n);
z[0] = 0;
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
if (i > r) {
l = r = i;
while (r < n && s[r] == s[r - l]) r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (i + z[k] - 1 < r)
z[i] = z[k];
else {
l = i;
while (r < n && s[r] == s[r - l]) r++;
z[i] = r - l;
r--;
}
}
}
return z;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s;
cin >> s;
n = s.length();
vector<int> z = getZ(s, n);
int prefix[n];
prefix[0] = 0;
for (int i = 1; i < n; i++) prefix[i] = max(prefix[i - 1], z[i]);
int ansStart = -1, ansLength = 0;
for (int i = n - 1; i >= 1; i--) {
int valRight = z[i];
if (valRight > 0 && valRight == n - i) {
if (prefix[i - 1] >= valRight) {
if (valRight > ansLength) {
ansStart = i;
ansLength = valRight;
}
}
}
}
if (ansStart == -1)
cout << "Just a legend\n";
else
cout << s.substr(ansStart, ansLength) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int h[1000005];
long long int pp[1000005];
long long int gethash(long long int i, long long int j) {
return (h[j + 1] - h[i] + 1000000009) % 1000000009;
}
bool check(long long int k, long long int n) {
cout << k << "\n";
long long int h1, h2, h3;
h1 = gethash(0, k - 1);
h2 = gethash(n - k, n - 1);
h1 = (h1 * pp[n - k]) % 1000000009;
h2 = (h2 * pp[0]) % 1000000009;
cout << h1 << " " << h2 << "\n";
if (h1 != h2) return false;
for (long long int i = k; i < n - 1; i++) {
h3 = gethash(i - k + 1, i);
h3 = (h3 * pp[0]) % 1000000009;
cout << h3 << "\n";
if (h3 == h1) return true;
}
return false;
}
void solve() {
string s;
cin >> s;
long long int n = s.length(), ans = 0;
vector<long long int> z(n, 0);
for (long long int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
if (z[i] + i - 1 > r) l = i, r = z[i] + i - 1;
}
long long int ma = 0, id = -1;
for (long long int i = 1; i < n - 1; i++) {
if (ma < z[i]) {
ma = z[i];
id = i;
}
}
for (long long int i = n - 1; i >= 0; i--) {
if (i + z[i] == n) {
if (ma > z[i])
ans = max(ans, z[i]);
else if (ma == z[i] && id + ma != n)
ans = max(ans, z[i]);
}
}
if (ans == 0)
cout << "Just a legend";
else
cout << s.substr(0, ans);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size(), p;
vector<int> pre(n, -1);
for (int i = 1; i < n; i++) {
p = i - 1;
while (s[pre[p] + 1] != s[i]) {
if (pre[p] == -1) {
break;
}
p = pre[p];
}
if (s[pre[p] + 1] == s[i]) {
pre[i] = pre[p] + 1;
} else {
pre[i] = -1;
}
}
set<int> T;
int i = pre[n - 1];
while (i != -1) {
T.insert(i + 1);
i = pre[i];
}
int x = -1;
for (int i = 1; i < n - 1; i++) {
if (T.find(pre[i] + 1) != T.end()) {
if (pre[i] + 1 > x) x = pre[i] + 1;
}
}
if (x == -1)
cout << "Just a legend\n";
else {
for (int i = 0; i < x; i++) {
cout << s[i];
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long B = 727;
const long long MAXn = 1e6 + 100;
string s;
long long n, hsh[MAXn], pw[MAXn];
vector<long long> v;
long long hs(long long l, long long r) {
if (l == 0) return hsh[r];
return (hsh[r] - ((hsh[l - 1] * pw[r - l + 1]) % MOD) + MOD) % MOD;
}
bool is_ok(long long x) {
if (hs(0, x) != hs(n - x - 1, n - 1)) return 0;
for (long long i = x + 1; i < n - 1; ++i)
if (hs(0, x) == hs(i - x, i)) return 1;
return 0;
}
long long bs(long long l, long long r) {
if (l == r) return r;
long long mid = (l + r + 1) / 2;
if (is_ok(v[mid]))
bs(mid, r);
else
bs(l, mid - 1);
}
signed main() {
cin >> s;
n = (long long)s.size();
pw[0] = 1;
for (long long i = 1; i < MAXn; ++i) {
pw[i] = (pw[i - 1] * B) % MOD;
}
hsh[0] = s[0];
for (long long i = 1; i < n; ++i) {
hsh[i] = ((hsh[i - 1] * B) % MOD + (s[i])) % MOD;
}
for (long long i = 0; i < n - 1; ++i)
if (hs(0, i) == hs(n - i - 1, n - 1)) v.push_back(i);
if ((long long)v.size() == 0) {
return cout << "Just a legend\n", 0;
}
long long nn = (long long)v.size();
long long point = bs(0, nn - 1);
if (is_ok(v[point])) {
for (long long i = 0; i <= v[point]; ++i) cout << s[i];
cout << endl;
} else {
return cout << "Just a legend\n", 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000007;
int phi[2][N];
void calc_phi(int id, string& s) {
phi[id][0] = phi[id][1] = 0;
for (int i = 2; i <= (int)s.size(); i++) {
int x = phi[id][i - 1];
while (x != 0 and s[x] != s[i - 1]) {
x = phi[id][x];
}
if (s[x] == s[i - 1])
phi[id][i] = x + 1;
else
phi[id][i] = 0;
}
}
int main() {
string s;
cin >> s;
string t = s;
reverse(t.begin(), t.end());
calc_phi(0, s);
calc_phi(1, t);
int best = 0, p = -1;
for (int i = 1, j = s.size(); i <= s.size(); ++i, --j) {
if (phi[0][i] == phi[1][j + phi[0][i] - 1] and phi[0][i] > best) {
best = phi[0][i];
p = i - phi[0][i];
}
}
if (best)
cout << s.substr(p, best) << endl;
else
cout << "Just a legend\n";
}
|
#include <bits/stdc++.h>
using namespace std;
const long long p = 1000000000007LL;
char buf[1000010];
long long hsh[1000010], pw[1000010];
long long gthsh(int l, int r) { return hsh[r] - hsh[l - 1] * pw[r - l + 1]; }
int main() {
scanf("%s", buf);
int n = strlen(buf);
pw[0] = 1LL;
for (int i = 0; i < n; i++) {
pw[i + 1] = pw[i] * p;
hsh[i + 1] = hsh[i] * p + buf[i];
}
for (int i = n - 1; i >= 0; i--) {
if (gthsh(1, i + 1) != gthsh(n - i, n)) continue;
long long t = gthsh(1, i + 1);
for (int j = 2; j + i < n; j++) {
if (gthsh(j, j + i) == t) {
for (int k = 0; k <= i; k++) {
printf("%c", buf[k]);
}
return 0;
}
}
}
printf("Just a legend");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int mod = 1000099999, b = 313, h, n, l, q;
string s;
vector<int> pref(1000001), power(1000001), d;
void precalc(const string &s) {
pref[0] = 0;
power[0] = 1;
for (int i = 0; i < s.size(); i++) {
pref[i + 1] = ((long long)pref[i] * b + s[i] - 'a' + 1) % mod;
power[i + 1] = ((long long)power[i] * b) % mod;
}
}
int hash_(int i, int j) {
return (mod + pref[j] - ((long long)pref[i] * power[j - i]) % mod) % mod;
}
bool f(int x) {
h = hash_(0, x);
int k = 0;
for (int i = 1; i < s.size() - x + 1; i++)
if (h == hash_(i, i + x)) k++;
return k > 1;
}
int main() {
cin >> s;
precalc(s);
n = s.size();
d.push_back(0);
for (int i = 1; i < s.size() - 1; i++) {
if (hash_(0, i) == hash_(n - i, n)) d.push_back(i);
}
int l = 0, r = d.size() - 1, m;
while (l < r) {
m = (l + r + 1) / 2;
if (f(d[m]))
l = m;
else
r = m - 1;
}
if (d[l] > 0) {
cout << s.substr(0, d[l]);
return 0;
}
cout << "Just a legend";
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-4;
const double pi = acos(-1.0);
void debug() { cerr << '\n'; }
template <typename T, typename... Ts>
void debug(T x, Ts... y) {
cerr << "\033[31m" << x << "\033[0m";
if (sizeof...(y) > 0) cerr << ' ';
debug(y...);
}
template <typename T>
void debug(const T &a, int l, int r, char c) {
for (int i = l; i <= r; ++i)
cerr << "\033[31m" << a[i] << "\033[0m" << (i == r ? '\n' : c);
}
template <typename T>
void debug(vector<T> a) {
for (int i = 0; i < (int)a.size(); ++i)
cerr << "\033[31m" << a[i] << "\033[31m"
<< " \n"[i == ((int)a.size() - 1)];
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long qpow(long long x, long long y) {
long long ans = 1;
for (; y > 0; y >>= 1) {
if (y & 1) ans *= x;
x *= x;
}
return ans;
}
long long qpow(long long x, long long y, int MOD) {
long long ans = 1;
for (; y > 0; y >>= 1) {
if (y & 1) ans = ans * x % MOD;
x = x * x % MOD;
}
return ans;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return;
}
exgcd(b, a % b, x, y);
long long X = x;
x = y;
y = X - a / b * y;
}
long long inv(long long x, int mod = MOD) { return qpow(x, mod - 2, mod); }
long long m_m(long long a, int mod = MOD) { return (a % mod + mod) % mod; }
int pre[1000005];
map<int, int> mp;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
string s;
cin >> s;
int n = s.size();
for (int i = (2); i < (n + 1); ++i) {
int j = pre[i - 1];
while (j > 0 && s[i - 1] != s[j]) j = pre[j];
if (s[i - 1] == s[j]) ++j;
pre[i] = j;
}
for (int i = (2); i < (n); ++i) mp[pre[i]] = 1;
int x = pre[n];
while (x) {
if (mp[x]) {
for (int i = (0); i < (x); ++i) cout << s[i];
return 0;
}
x = pre[x];
}
cout << "Just a legend";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int n = s.length();
int f[n], len = 0, i = 1, c = 0, j = 0;
f[0] = 0;
while (i < n) {
if (s[len] == s[i])
f[i++] = ++len;
else {
if (len != 0)
len = f[len - 1];
else
f[i++] = 0;
}
if (i >= n) break;
}
string pattern = s.substr(0, f[n - 1]);
int m = f[n - 1];
i = 0, j = 0;
int l = 0, r = m, mid;
while (l < r) {
mid = l + (r - l + 1) / 2;
pattern = s.substr(0, mid);
c = 0, i = 0, j = 0;
while (i < n) {
if (pattern[j] == s[i])
i++, j++;
else {
if (j != 0)
j = f[j - 1];
else
i++;
}
if (j == mid) {
c++, j = f[j - 1];
}
if (i >= n) break;
}
if (c >= 3)
l = mid;
else
r = mid - 1;
}
pattern = s.substr(0, l);
reverse(s.begin(), s.end());
string p2 = s.substr(0, l);
reverse(p2.begin(), p2.end());
if (l > 0 && pattern == p2)
cout << pattern << endl;
else
cout << "Just a legend" << endl;
}
int main() {
ios::sync_with_stdio(false);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
const int N = 1e6 + 7;
char s[N];
int ans, nxt[N], i, j, n, vis[N];
int main() {
for (scanf("%s", s + 1), vis[0] = true, n = strlen(s + 1), i = 2, j = 0;
i <= n; ++i) {
while (j && s[j + 1] != s[i]) j = nxt[j];
if (s[j + 1] == s[i]) j++;
nxt[i] = j;
if (i < n) vis[j] = true;
}
for (ans = nxt[n]; ans && !vis[ans]; ans = nxt[ans])
;
s[ans + 1] = '\0';
return 0 * puts(ans == 0 ? "Just a legend" : s + 1);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
int lps[n];
lps[0] = 0;
int len = 0;
int i = 1;
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
if (!lps[n - 1]) {
cout << "Just a legend" << endl;
return 0;
}
for (int i = 1; i < n - 1; i++) {
if (lps[i] == lps[n - 1]) {
cout << s.substr(0, lps[i]) << endl;
return 0;
}
}
s = s.substr(0, lps[n - 1]);
n = s.size();
lps[0] = 0;
len = 0;
i = 1;
while (i < n) {
if (s[i] == s[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
if (lps[n - 1]) {
cout << s.substr(0, lps[n - 1]) << endl;
return 0;
}
cout << "Just a legend" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 9;
const long long mod = 1e9 + 7;
int f[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
f[0] = f[1] = 0;
int k = 0;
for (int i = 2; i <= s.length(); i++) {
while (k != 0 && s[i - 1] != s[k]) k = f[k];
if (s[i - 1] == s[k]) k++;
f[i] = k;
}
for (int i = 1; i < s.length(); i++)
if (f[i] == f[s.length()] && f[s.length()] != 0) {
for (int t = 0; t < f[i]; t++) cout << s[t];
cout << "\n";
return 0;
}
if (f[f[s.length()]]) {
for (int j = 0; j < f[f[s.length()]]; j++) cout << s[j];
cout << "\n";
return 0;
}
cout << "Just a legend\n";
}
|
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1791791791;
double eps = 1e-12;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T, typename U, typename V>
struct Triple {
T first;
U second;
V third;
Triple(T first, U second, V third)
: first(first), second(second), third(third) {}
Triple() {}
};
const long long p = 31, mod = 1791791791;
vector<long long> ps, h;
long long n;
long long subHash(long long l, long long r) {
return (h[r + 1] - (h[l] * ps[r - l + 1]) % mod + mod) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
n = s.size();
ps.resize(n + 1);
h.resize(n + 1);
ps[0] = 1;
h[0] = 0;
for (long long i = 0; i < n; i++) {
ps[i + 1] = (ps[i] * p) % mod;
long long d = (s[i] - 'a' + 1);
h[i + 1] = (h[i] * p + d) % mod;
}
vector<pair<long long, long long> > finds;
for (long long i = 0; i < n - 1; i++) {
if (h[i + 1] == subHash(n - i - 1, n - 1)) {
finds.push_back({i + 1, h[i + 1]});
}
}
long long l = -1, r = finds.size();
long long ans = 0;
while (l + 1 < r) {
long long m = (l + r) / 2;
bool done = false;
long long need = finds[m].second;
for (long long i = 1; i < n - finds[m].first; i++) {
if (need == subHash(i, i + finds[m].first - 1)) {
done = true;
}
}
if (done) {
l = m;
ans = finds[m].first;
} else {
r = m;
}
}
if (ans == 0) {
cout << "Just a legend"
<< "\n";
} else {
cout << s.substr(0, ans) << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool op[3][100002];
int t;
bool check(int c) {
if (!op[1][c] && (!op[2][c] || !op[2][c - 1] || !op[2][c + 1])) {
return false;
} else if (!op[2][c] && (!op[1][c] || !op[1][c - 1] || !op[1][c + 1])) {
return false;
}
return true;
}
int main() {
memset(op, true, sizeof(op));
int n, q, r, c;
cin >> n >> q;
for (int i = 0; i < q; i++) {
cin >> r >> c;
bool n1 = check(c);
bool n2 = check(c - 1);
bool n3 = check(c + 1);
op[r][c] = (op[r][c] ? false : true);
t += int((n1 ? 1 : 0) - (check(c) ? 1 : 0));
t += int((n2 ? 1 : 0) - (check(c - 1) ? 1 : 0));
t += int((n3 ? 1 : 0) - (check(c + 1) ? 1 : 0));
cout << (t == 0 ? "Yes" : "No") << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9 + 7);
vector<vector<bool> > mapa;
int c = 0;
void update(int x, int y) {
int add;
if (mapa[x][y])
add = -1;
else
add = +1;
mapa[x][y] = !mapa[x][y];
if (x == 2)
x = 1;
else
x = 2;
if (mapa[x][y]) c += add;
if (mapa[x][y - 1]) c += add;
if (mapa[x][y + 1]) c += add;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q, x, y;
cin >> n >> q;
mapa.assign(3, vector<bool>(n + 2, false));
while (q--) {
cin >> x >> y;
update(x, y);
if (c == 0)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char* s) { return to_string((string)s); }
string to_string(const char c) {
string s = "'";
s += c;
s += "'";
return s;
}
template <class T, class U>
string to_string(const pair<T, U>& p) {
string s = "(";
s += to_string(p.first);
s += ", ";
s += to_string(p.second);
s += ")";
return s;
}
template <class T>
string to_string(const T& a) {
bool first = true;
string s = "{";
for (const auto& x : a) {
if (!first) {
s += ", ";
}
first = false;
s += to_string(x);
}
s += "}";
return s;
}
void debug_out() { cerr << '\n'; }
template <class Head, class... Tail>
void debug_out(Head head, Tail... tail) {
cerr << ' ' << to_string(head);
debug_out(tail...);
}
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int inf = 1e9 + 100;
const ll inf64 = 7e18l;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n, q;
cin >> n >> q;
vector<pair<int, int>> a(q);
for (int i = 0; i < q; ++i) {
cin >> a[i].first >> a[i].second;
--a[i].first;
--a[i].second;
}
vector<vector<int>> now(2, vector<int>(n));
int bad = 0;
for (int i = 0; i < q; ++i) {
int x = a[i].first, y = a[i].second;
if (now[x][y] == 0) {
int op = x ^ 1;
if (y - 1 >= 0 && now[op][y - 1] == 1) {
++bad;
}
if (y + 1 < n && now[op][y + 1] == 1) {
++bad;
}
if (now[op][y] == 1) {
++bad;
}
now[x][y] = 1;
} else {
int op = x ^ 1;
if (y - 1 >= 0 && now[op][y - 1] == 1) {
--bad;
}
if (y + 1 < n && now[op][y + 1] == 1) {
--bad;
}
if (now[op][y] == 1) {
--bad;
}
now[x][y] = 0;
}
if (bad == 0) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(steady_clock::now().time_since_epoch().count());
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const int mod = 1000000007;
long long n, m, k, q, Q, T, l, r, x, y, z;
long long a[1000005];
long long b[1000005];
long long c[1000005];
string second, t;
long long ans = 0;
bool mat[2][100005];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
};
cin >> n >> q;
int cnt = 0;
memset(mat, 0, sizeof(mat));
for (long long i = 0; i < q; i++) {
cin >> x >> y;
--x;
--y;
if (mat[x][y]) {
if (y != 0) cnt -= mat[x ^ 1][y - 1];
cnt -= mat[x ^ 1][y];
if (y != n - 1) cnt -= mat[x ^ 1][y + 1];
} else {
if (y != 0) cnt += mat[x ^ 1][y - 1];
cnt += mat[x ^ 1][y];
if (y != n - 1) cnt += mat[x ^ 1][y + 1];
}
mat[x][y] ^= 1;
cout << (cnt == 0 ? "Yes" : "No") << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
bool* uno;
bool* dos;
long long int nblocks = 0;
long long int n, q;
cin >> n >> q;
uno = new bool[n + 2];
dos = new bool[n + 2];
memset(uno, 1, n + 2);
memset(dos, 1, n + 2);
for (int i = 0; i < q; i++) {
int a;
long long int b;
cin >> a >> b;
if (a == 1) {
uno[b] = !uno[b];
if (!dos[b - 1]) {
if (!uno[b])
nblocks++;
else
nblocks--;
}
if (!dos[b]) {
if (!uno[b])
nblocks++;
else
nblocks--;
}
if (!dos[b + 1]) {
if (!uno[b])
nblocks++;
else
nblocks--;
}
} else {
dos[b] = !dos[b];
if (!uno[b - 1]) {
if (!dos[b])
nblocks++;
else
nblocks--;
}
if (!uno[b]) {
if (!dos[b])
nblocks++;
else
nblocks--;
}
if (!uno[b + 1]) {
if (!dos[b])
nblocks++;
else
nblocks--;
}
}
if (!nblocks)
cout << "yes\n";
else
cout << "no\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int i, j, k;
int main() {
int n;
cin >> n;
;
int q;
cin >> q;
;
int a[3][n + 1];
int f = 0;
for (int i = (0); i < (3); i++)
for (int j = (0); j < (n + 1); j++) a[i][j] = 1;
for (i = 0; i < q; i++) {
int x;
cin >> x;
;
int y;
cin >> y;
;
a[x][y] ^= 1;
if (x == 1) {
if (a[x][y] == 0 and a[x + 1][y] == 0) f++;
if (a[x][y] == 1 and a[x + 1][y] == 0) f--;
if (a[x][y] == 0 and a[x + 1][y + 1] == 0 and y + 1 <= n) f++;
if (a[x][y] == 0 and a[x + 1][y - 1] == 0 and y - 1 >= 1) f++;
if (a[x][y] == 1 and a[x + 1][y + 1] == 0 and y + 1 <= n) f--;
if (a[x][y] == 1 and a[x + 1][y - 1] == 0 and y - 1 >= 1) f--;
} else {
if (a[x][y] == 0 and a[x - 1][y] == 0) f++;
if (a[x][y] == 1 and a[x - 1][y] == 0) f--;
if (a[x][y] == 0 and a[x - 1][y + 1] == 0 and y + 1 <= n) f++;
if (a[x][y] == 0 and a[x - 1][y - 1] == 0 and y - 1 >= 1) f++;
if (a[x][y] == 1 and a[x - 1][y + 1] == 0 and y + 1 <= n) f--;
if (a[x][y] == 1 and a[x - 1][y - 1] == 0 and y - 1 >= 1) f--;
}
f > 0 ? cout << "NO\n" : cout << "YES\n";
}
}
|
#include <bits/stdc++.h>
const int MaxN = 2e5 + 10;
const int dx[] = {-1, 0, 1};
int n, q, cnt;
int a[2][MaxN];
inline int read() {
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0') ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
int main() {
n = read(), q = read();
while (q--) {
int r = read() - 1, c = read();
if (a[r][c]) {
a[r][c] = 0;
for (int i = 0; i < 3; i++)
if (a[r ^ 1][c + dx[i]]) --cnt;
} else {
a[r][c] = 1;
for (int i = 0; i < 3; i++)
if (a[r ^ 1][c + dx[i]]) ++cnt;
}
printf(cnt ? "No\n" : "Yes\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int grid[3][100005];
int s = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
while (q--) {
int t1, t2;
cin >> t1 >> t2;
int r = grid[t1][t2] ? -1 : 1;
s += grid[3 - t1][t2] * r;
s += grid[3 - t1][t2 - 1] * r;
s += grid[3 - t1][t2 + 1] * r;
if (!s)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
grid[t1][t2] = 1 - grid[t1][t2];
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, q, cnt1, cnt2;
bool flag[2][1000005];
int read() {
char c = getchar();
int ans = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar();
return ans;
}
void del(int x, int y) {
cnt1 -= flag[x ^ 1][y],
cnt2 -= (y && flag[x ^ 1][y - 1]) + (y < n && flag[x ^ 1][y + 1]);
return;
}
void ins(int x, int y) {
cnt1 += flag[x ^ 1][y],
cnt2 += (y && flag[x ^ 1][y - 1]) + (y < n && flag[x ^ 1][y + 1]);
return;
}
int main() {
n = read(), q = read();
for (register int i = 1, x, y; i <= q; i++) {
x = read() - 1, y = read();
if (flag[x][y])
del(x, y), flag[x][y] = 0;
else
ins(x, y), flag[x][y] = 1;
if (!cnt1 && !cnt2)
putchar('Y'), putchar('E'), putchar('S'), putchar('\n');
else
putchar('N'), putchar('O'), putchar('\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
scanf("%d %d", &n, &q);
vector<bool> tab[2];
tab[0].assign(n + 2, false);
tab[1].assign(n + 2, false);
int prob = 0;
for (int i = 0; i < q; i++) {
int r, c;
scanf("%d %d", &r, &c);
r--;
if (tab[r][c]) {
tab[r][c] = false;
for (int j = -1; j <= 1; j++) prob -= (tab[(r + 1) % 2][c + j]);
} else {
tab[r][c] = true;
for (int j = -1; j <= 1; j++) prob += (tab[(r + 1) % 2][c + j]);
}
if (prob)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, q, s;
int i, j;
int a[2][100005];
int main() {
scanf("%d%d", &n, &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
x--;
if (a[x][y]) {
s -= a[x ^ 1][y - 1];
s -= a[x ^ 1][y];
s -= a[x ^ 1][y + 1];
a[x][y] ^= 1;
} else {
s += a[x ^ 1][y - 1];
s += a[x ^ 1][y];
s += a[x ^ 1][y + 1];
a[x][y] ^= 1;
}
if (s) {
puts("No");
} else {
puts("Yes");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
return (a.first < b.first) || (a.first == b.first && a.second > b.second);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
int n;
cin >> n;
int q;
cin >> q;
int a[3][n + 1];
int d = 0;
for (int i = 1; i <= n; i++) a[1][i] = 0, a[2][i] = 0;
while (q--) {
int r, c;
cin >> r >> c;
if (!a[r][c]) {
a[r][c] = 1;
if (r == 1) {
if (c == n) {
if (a[2][n - 1]) d++;
} else {
if (a[2][c]) d++;
if (a[2][c - 1]) d++;
if (a[2][c + 1]) d++;
}
} else {
if (c == 1) {
if (a[1][2]) d++;
} else {
if (a[1][c]) d++;
if (a[1][c - 1]) d++;
if (a[1][c + 1]) d++;
}
}
} else {
a[r][c] = 0;
if (r == 1) {
if (c == n) {
if (a[2][n - 1]) d--;
} else {
if (a[2][c]) d--;
if (a[2][c - 1]) d--;
if (a[2][c + 1]) d--;
}
} else {
if (c == 1) {
if (a[1][2]) d--;
} else {
if (a[1][c]) d--;
if (a[1][c - 1]) d--;
if (a[1][c + 1]) d--;
}
}
}
if (d == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c *r) -> decltype(cerr << *r);
template <class c>
char dud(...);
struct muu {
template <class c>
muu &operator<<(const c &) {
return *this;
}
};
using pii = pair<int, int>;
using ld = long double;
using ll = long long;
bool zajete[3][100007];
int main() {
int N, Q;
scanf("%d%d", &N, &Q);
int ile = 0;
while (Q--) {
int a, b;
scanf("%d%d", &a, &b);
a--;
int zle = 0;
zle += zajete[!a][b - 1];
zle += zajete[!a][b];
zle += zajete[!a][b + 1];
zajete[a][b] = !zajete[a][b];
if (zajete[a][b])
ile += zle;
else
ile -= zle;
muu() << __FUNCTION__ << "#" << 62 << ": "
<< "["
"ile"
": "
<< (ile)
<< "] "
"["
"zle"
": "
<< (zle) << "] ";
muu() << __FUNCTION__ << "#" << 64 << ": " << zajete[a][b];
if (ile == 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
int arr[2][1000005];
memset(arr, 0, sizeof(arr));
int cnt = 0;
while (q--) {
int x, y;
cin >> x >> y;
x--;
arr[x][y] = arr[x][y] ^ 1;
if (arr[x][y]) {
for (long long j = y - 1; j < y + 2; j++) {
if (arr[x ^ 1][j]) cnt++;
}
} else {
for (long long j = y - 1; j < y + 2; j++) {
if (arr[x ^ 1][j]) cnt--;
}
}
if (cnt == 0)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string alpha = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1);
const int N = 1e7 + 5;
long long int MOD = 1e9 + 7;
long long int OO = 1e18 + 8;
int oo = 1e9 + 9;
long long int pw(long long int b, long long int p) {
if (!p) return 1;
long long int sq = pw(b, p / 2);
sq *= sq;
if (p % 2) sq *= b;
return sq;
}
long long int gcd(long long int a, long long int b) {
return (b == 0 ? a : gcd(b, a % b));
}
long long int lcm(long long int a, long long int b) {
return ((a * b) / gcd(a, b));
}
long long int PowMod(long long int bs, long long int ex, long long int M) {
if (!ex) return 1;
long long int p = PowMod((bs * bs) % M, ex >> 1, M);
return (ex & 1) ? (p * bs) % M : p;
}
long long int ModInverse(long long int a) { return PowMod(a, MOD - 2, MOD); }
long long int ExtGCD(long long int a, long long int b, long long int& x,
long long int& y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long int r = ExtGCD(b, a % b, y, x);
y -= a / b * x;
return r;
}
long long int mult(long long int a, long long int b) {
return (1LL * a * b) % MOD;
}
bool cmp(double a, double b) { return abs(a - b) < 1e-9; }
bool isint(double a) { return cmp(a, round(a)); }
bool isPrime(long long int n) {
if (n == 2) return 1;
if (n < 2 || n % 2 == 0) return 0;
for (long long int i = 3; i * i <= n; i += 2)
if (n % i == 0) return 0;
return 1;
}
bool isPalindrome(string str) {
long long int l = 0;
long long int h = str.size() - 1;
while (h > l) {
if (str[l++] != str[h--]) {
return 0;
}
}
return 1;
}
bool isPerfectSquare(long double x) {
long double sr = sqrt(x);
return ((sr - floor(sr)) == 0);
}
double EucildDistance(double x, double y, double xx, double yy) {
return sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy));
};
long long int n, q;
map<pair<int, int>, bool> m;
map<pair<pair<int, int>, pair<int, int> >, bool> block;
int main() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
;
cin >> n >> q;
while (q--) {
int r, c;
cin >> r >> c;
if (m.count({r, c})) {
m.erase({r, c});
if (block.count({{r - 1, c - 1}, {r, c}}))
block.erase({{r - 1, c - 1}, {r, c}});
if (block.count({{r, c}, {r - 1, c - 1}}))
block.erase({{r, c}, {r - 1, c - 1}});
if (block.count({{r - 1, c}, {r, c}})) block.erase({{r - 1, c}, {r, c}});
if (block.count({{r, c}, {r - 1, c}})) block.erase({{r, c}, {r - 1, c}});
if (block.count({{r - 1, c + 1}, {r, c}}))
block.erase({{r - 1, c + 1}, {r, c}});
if (block.count({{r, c}, {r - 1, c + 1}}))
block.erase({{r, c}, {r - 1, c + 1}});
if (block.count({{r + 1, c + 1}, {r, c}}))
block.erase({{r + 1, c + 1}, {r, c}});
if (block.count({{r, c}, {r + 1, c + 1}}))
block.erase({{r, c}, {r + 1, c + 1}});
if (block.count({{r + 1, c}, {r, c}})) block.erase({{r + 1, c}, {r, c}});
if (block.count({{r, c}, {r + 1, c}})) block.erase({{r, c}, {r + 1, c}});
if (block.count({{r + 1, c - 1}, {r, c}}))
block.erase({{r + 1, c - 1}, {r, c}});
if (block.count({{r, c}, {r + 1, c - 1}}))
block.erase({{r, c}, {r + 1, c - 1}});
} else {
m[{r, c}];
if (m.count({r - 1, c - 1})) block[{{r - 1, c - 1}, {r, c}}] = 1;
if (m.count({r - 1, c})) block[{{r - 1, c}, {r, c}}] = 1;
if (m.count({r - 1, c + 1})) block[{{r - 1, c + 1}, {r, c}}] = 1;
if (m.count({r + 1, c + 1})) block[{{r + 1, c + 1}, {r, c}}] = 1;
if (m.count({r + 1, c})) block[{{r + 1, c}, {r, c}}] = 1;
if (m.count({r + 1, c - 1})) block[{{r + 1, c - 1}, {r, c}}] = 1;
}
if (block.size() == 0)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int mod = (int)1e9 + 7;
int main() {
int n, q;
cin >> n >> q;
vector<vector<int> > a(2, vector<int>(n));
int bad = 0;
int row, column;
while (q--) {
cin >> row >> column;
row--;
column--;
int count = 0, adj = (row + 1) % 2;
for (int i = max(0, column - 1); i <= min(n - 1, column + 1); i++)
count += (a[adj][i] == 1);
if (a[row][column] == 1) {
bad -= count;
a[row][column] = 0;
} else {
bad += count;
a[row][column] = 1;
}
if (bad > 0)
cout << "No\n";
else
cout << "Yes\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n, q, x, y;
bool lava[2][(int)1e5 + 3];
string get_string() {
string s;
getline(cin >> ws, s);
return s;
}
void solve() {
long long int change, total = 0;
cin >> n >> q;
memset(lava, false, sizeof(lava));
for (long long int i = 0; i < q; i++) {
cin >> x >> y;
x--, y--;
lava[x][y] = !lava[x][y];
if (lava[x][y]) {
change = 1;
} else {
change = -1;
}
for (long long int j = -1; j <= 1; j++) {
if (y + j < 0 || y + j >= n) {
continue;
}
if (lava[1 - x][y + j]) {
total += change;
}
}
if (total == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << showpoint;
cout << setprecision(9);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
set<pair<int, int> > isFlipped;
int x, y;
int prev = 1;
set<pair<int, int> > st;
map<pair<int, int>, vector<int> > mp;
int f = 0;
for (int i = 0; i < q; i++) {
cin >> x >> y;
f = 0;
if (isFlipped.find(make_pair(x, y)) != isFlipped.end()) {
isFlipped.erase(make_pair(x, y));
f = 1;
} else {
isFlipped.insert(make_pair(x, y));
}
if (f == 0) {
st.insert(make_pair(x, y));
if (x == 1) {
if (st.find(make_pair(x + 1, y)) != st.end()) {
if (mp.find(make_pair(1, y)) != mp.end()) {
mp[make_pair(1, y)][0] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y)] = t;
mp[make_pair(1, y)][0] = 1;
}
}
if (st.find(make_pair(x + 1, y - 1)) != st.end()) {
if (mp.find(make_pair(1, y)) != mp.end()) {
mp[make_pair(1, y)][1] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y)] = t;
mp[make_pair(1, y)][1] = 1;
}
}
if (st.find(make_pair(2, y + 1)) != st.end()) {
if (mp.find(make_pair(1, y)) != mp.end()) {
mp[make_pair(1, y)][2] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y)] = t;
mp[make_pair(1, y)][2] = 1;
}
}
} else {
if (st.find(make_pair(1, y)) != st.end()) {
if (mp.find(make_pair(1, y)) != mp.end()) {
mp[make_pair(1, y)][0] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y)] = t;
mp[make_pair(1, y)][0] = 1;
}
}
if (st.find(make_pair(1, y + 1)) != st.end()) {
if (mp.find(make_pair(1, y + 1)) != mp.end()) {
mp[make_pair(1, y + 1)][1] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y + 1)] = t;
mp[make_pair(1, y + 1)][1] = 1;
}
}
if (st.find(make_pair(1, y - 1)) != st.end()) {
if (mp.find(make_pair(1, y - 1)) != mp.end()) {
mp[make_pair(1, y - 1)][2] = 1;
} else {
vector<int> t(3, 0);
mp[make_pair(1, y - 1)] = t;
mp[make_pair(1, y - 1)][2] = 1;
}
}
}
} else {
st.erase(make_pair(x, y));
if (x == 1 && mp.find(make_pair(x, y)) != mp.end()) {
mp.erase(make_pair(x, y));
}
if (x == 2) {
if (isFlipped.find(make_pair(1, y)) != isFlipped.end()) {
mp[make_pair(1, y)][0] = 0;
if (mp[make_pair(1, y)][1] == 0 && mp[make_pair(1, y)][2] == 0)
mp.erase(make_pair(1, y));
}
if (isFlipped.find(make_pair(1, y + 1)) != isFlipped.end()) {
mp[make_pair(1, y + 1)][1] = 0;
if (mp[make_pair(1, y + 1)][0] == 0 &&
mp[make_pair(1, y + 1)][2] == 0)
mp.erase(make_pair(1, y + 1));
}
if (isFlipped.find(make_pair(1, y - 1)) != isFlipped.end()) {
mp[make_pair(1, y - 1)][2] = 0;
if (mp[make_pair(1, y - 1)][0] == 0 &&
mp[make_pair(1, y - 1)][1] == 0)
mp.erase(make_pair(1, y - 1));
}
}
}
if (mp.size() == 0) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, c, k, n, q, r;
k = 0;
cin >> n >> q;
int a[2][n];
for (i = 0; i < n; i++) a[0][i] = a[1][i] = 0;
while (q--) {
cin >> r >> c;
r--;
c--;
if (a[r][c] == 0) {
a[r][c] = 1;
if (a[(r + 1) % 2][c] == 1) k++;
if (c > 0) {
if (a[(r + 1) % 2][c - 1] == 1) k++;
}
if (c < (n - 1)) {
if (a[(r + 1) % 2][c + 1] == 1) k++;
}
} else {
a[r][c] = 0;
if (a[(r + 1) % 2][c] == 1) k--;
if (c > 0) {
if (a[(r + 1) % 2][c - 1] == 1) k--;
}
if (c < (n - 1)) {
if (a[(r + 1) % 2][c + 1] == 1) k--;
}
}
if (k > 0)
cout << "No\n";
else
cout << "Yes\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n = 0, q = 0, flag = 0, temp = 0;
int dp[3][100005] = {0};
cin >> n >> q;
while (q--) {
int x, y;
cin >> x >> y;
dp[x][y] = (dp[x][y] ? 0 : 1);
if (x == 1) {
temp = dp[2][y + 1] + dp[2][y - 1] + dp[2][y];
if (dp[1][y]) {
flag += temp;
} else
flag -= temp;
} else {
temp = dp[1][y + 1] + dp[1][y - 1] + dp[1][y];
if (dp[2][y]) {
flag += temp;
} else
flag -= temp;
}
if (flag)
cout << "No\n";
else
cout << "Yes\n";
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, q, x, y, c;
bool b[100005][3];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
while (q--) {
cin >> y >> x;
if (b[x][y]) {
for (long long i = x - 1; i <= x + 1; i++) {
if (b[i][3 - y]) {
c--;
}
}
b[x][y] = false;
} else {
for (long long i = x - 1; i <= x + 1; i++) {
if (b[i][3 - y]) {
c++;
}
}
b[x][y] = true;
}
if (c == 0)
cout << "Yes\n";
else
cout << "No\n";
}
}
|
#include <bits/stdc++.h>
void err() { std::cout << std::endl; }
template <typename T, typename... Args>
void err(T a, Args... args) {
std::cout << a << ' ';
err(args...);
}
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 22;
int a[3][maxn];
int n, q, r, c, s1, s2;
int main() {
scanf("%d%d", &n, &q);
bool flag = 1;
while (q--) {
scanf("%d%d", &r, &c);
r--;
if (a[r][c]) {
a[r][c] = 0;
if (a[r ^ 1][c] == 1) s2--;
if (c - 1 >= 1 && a[r ^ 1][c - 1] == 1) s1--;
if (c + 1 <= n && a[r ^ 1][c + 1] == 1) s1--;
} else {
a[r][c] = 1;
if (a[r ^ 1][c] == 1) s2++;
if (c - 1 >= 1 && a[r ^ 1][c - 1] == 1) s1++;
if (c + 1 <= n && a[r ^ 1][c + 1] == 1) s1++;
}
if (!s1 && !s2)
puts("Yes");
else
puts("No");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 100100 * 2;
long long n, q, f;
bool a[3][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
while (q--) {
long long i, j;
cin >> i >> j;
a[i][j] = !a[i][j];
if (a[i][j]) {
if (a[3 - i][j - 1]) f++;
if (a[3 - i][j]) f++;
if (a[3 - i][j + 1]) f++;
} else {
if (a[3 - i][j - 1]) f--;
if (a[3 - i][j]) f--;
if (a[3 - i][j + 1]) f--;
}
if (!f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long n, i, j, ar[300005], d, a, b, m, l, k, pt, q, sz[300005], c0, d0, c,
r;
long long dp[300005], f = 0;
long long mrk[3][300005];
pair<long long, pair<long long, long long>> p[300005];
vector<int> v, vt;
int main() {
scanf("%lld %lld", &n, &q);
set<pair<int, int>> vis;
set<pair<pair<int, int>, pair<int, int>>> tot;
for (long long i = 0; i < q; i++) {
scanf("%lld %lld", &r, &c);
int f = 0;
if (vis.find({r, c}) == vis.end() && f == 0) {
f = 1;
vis.insert({r, c});
if (r == 2)
for (j = c - 1; j <= c + 1; j++) {
if (j < 1 || j > n) continue;
if (vis.find({r - 1, j}) != vis.end())
tot.insert({{r - 1, j}, {r, c}});
}
if (r == 1)
for (j = c - 1; j <= c + 1; j++) {
if (j < 1 || j > n) continue;
if (vis.find({r + 1, j}) != vis.end())
tot.insert({{r, c}, {r + 1, j}});
}
}
if (vis.find({r, c}) != vis.end() && f == 0) {
auto it = vis.find({r, c});
if (it != vis.end()) vis.erase({r, c});
for (j = c - 1; j <= c + 1; j++) {
if (j < 1 || j > n) continue;
if (r == 1) {
tot.erase({{r, c}, {r + 1, j}});
}
if (r == 2) {
tot.erase({{r - 1, j}, {r, c}});
}
}
}
if (tot.size() == 0)
cout << "Yes\n";
else
cout << "No\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t, n, block = 0;
cin >> n >> t;
map<pair<long long int, long long int>, long long int> mape, mape1;
while (t--) {
long long int r, c, forb;
cin >> r >> c;
if (mape.find({r, c}) == mape.end()) {
mape[{r, c}]++;
if (r == 1) {
if (mape.find({r + 1, c}) != mape.end()) block++;
if (mape.find({r + 1, c + 1}) != mape.end()) block++;
if (mape.find({r + 1, c - 1}) != mape.end()) block++;
} else {
if (mape.find({r - 1, c}) != mape.end()) block++;
if (mape.find({r - 1, c + 1}) != mape.end()) block++;
if (mape.find({r - 1, c - 1}) != mape.end()) block++;
}
if (block)
cout << "No\n";
else
cout << "Yes\n";
} else {
mape.erase({r, c});
if (r == 1) {
if (mape.find({r + 1, c}) != mape.end()) block--;
if (mape.find({r + 1, c + 1}) != mape.end()) block--;
if (mape.find({r + 1, c - 1}) != mape.end()) block--;
} else {
if (mape.find({r - 1, c}) != mape.end()) block--;
if (mape.find({r - 1, c + 1}) != mape.end()) block--;
if (mape.find({r - 1, c - 1}) != mape.end()) block--;
}
if (block)
cout << "No\n";
else
cout << "Yes\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
bool a[maxn], b[maxn];
int n, q;
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
int ans = 0;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
if (x == 1) {
if (a[y]) {
a[y] = 0;
if (b[y]) ans--;
if (b[y + 1]) ans--;
if (b[y - 1]) ans--;
} else {
a[y] = 1;
if (b[y]) ans++;
if (b[y + 1]) ans++;
if (b[y - 1]) ans++;
}
} else {
if (b[y]) {
b[y] = 0;
if (a[y]) ans--;
if (a[y + 1]) ans--;
if (a[y - 1]) ans--;
} else {
b[y] = 1;
if (a[y]) ans++;
if (a[y + 1]) ans++;
if (a[y - 1]) ans++;
}
}
if (ans)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<vector<bool>> states(2, vector<bool>(n, false));
int impossiblePairs = 0;
for (int i = 0; i < q; i++) {
int r, c;
cin >> r >> c;
r--;
c--;
if (states[r][c] == false) {
states[r][c] = true;
if (r == 0) {
if (c > 0 && states[1][c - 1] == true) {
impossiblePairs++;
}
if (states[1][c] == true) {
impossiblePairs++;
}
if (c < n - 1 && states[1][c + 1] == true) {
impossiblePairs++;
}
} else {
if (c > 0 && states[0][c - 1] == true) {
impossiblePairs++;
}
if (states[0][c] == true) {
impossiblePairs++;
}
if (c < n - 1 && states[0][c + 1] == true) {
impossiblePairs++;
}
}
} else {
states[r][c] = false;
if (r == 0) {
if (c > 0 && states[1][c - 1] == true) {
impossiblePairs--;
}
if (states[1][c] == true) {
impossiblePairs--;
}
if (c < n - 1 && states[1][c + 1] == true) {
impossiblePairs--;
}
} else {
if (c > 0 && states[0][c - 1] == true) {
impossiblePairs--;
}
if (states[0][c] == true) {
impossiblePairs--;
}
if (c < n - 1 && states[0][c + 1] == true) {
impossiblePairs--;
}
}
}
if (impossiblePairs > 0) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
bool RELAXED = false;
template <int, typename T>
struct MINMAX {
T val;
MINMAX(T val) : val(val) {}
};
template <typename T>
MINMAX<1, T> MAX(T val) {
return MINMAX<1, T>(val);
};
template <typename T>
MINMAX<2, T> MIN(T val) {
return MINMAX<2, T>(val);
};
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<1, U> rhs) {
return lhs = (rhs.val > lhs ? (RELAXED = true, rhs.val)
: (RELAXED = false, lhs));
}
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<2, U> rhs) {
return lhs = (rhs.val < lhs ? (RELAXED = true, rhs.val)
: (RELAXED = false, lhs));
}
template <typename T>
inline vector<T> readVector(int n) {
vector<T> vec(n);
for (int i = 0; i < int(n); i++) cin >> vec[i];
return vec;
}
template <typename T>
void EXIT(T answer) {
cout << answer << endl;
exit(0);
}
int lava[3][100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n >> q;
set<pair<int, int> > block1, block2;
for (int i = 0; i < int(q); i++) {
int r, c;
cin >> r >> c;
lava[r][c] ^= 1;
int ro = 2 - r + 1;
if (lava[r][c] && lava[ro][c])
block1.insert({r, c});
else {
block1.erase({r, c});
block1.erase({ro, c});
}
if (lava[r][c] && lava[ro][c + 1])
block2.insert({r, c});
else
block2.erase({r, c});
if (lava[r][c] && lava[ro][c - 1])
block2.insert({ro, c - 1});
else
block2.erase({ro, c - 1});
cout << (block1.size() + block2.size() ? "No" : "Yes") << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
int a[3][100100];
int n, q, x, y;
int main(void) {
scanf("%d %d", &n, &q);
int flag = 0;
while (q--) {
scanf("%d %d", &x, &y);
if (!a[x][y]) {
a[x][y] = 1;
if (a[3 - x][y + 1]) flag++;
if (a[3 - x][y]) flag++;
if (a[3 - x][y - 1]) flag++;
} else {
if (a[3 - x][y + 1]) flag--;
if (a[3 - x][y]) flag--;
if (a[3 - x][y - 1]) flag--;
a[x][y] = 0;
}
if (flag)
printf("No\n");
else
printf("Yes\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, q;
cin >> n >> q;
vector<vector<bool>> path(3, vector<bool>(n + 2, false));
long long cnt = 0;
for (long long i = 0; i < q; i++) {
long long x, y;
cin >> x >> y;
path[x][y] = !path[x][y];
if ((path[x][y] and path[3 - x][y + 1])) {
cnt++;
}
if ((path[3 - x][y - 1] and path[x][y])) {
cnt++;
}
if ((path[x][y] and path[3 - x][y])) {
cnt++;
}
if (cnt > 0 and !path[x][y] and path[3 - x][y + 1]) cnt--;
if (cnt > 0 and !path[x][y] and path[3 - x][y - 1]) cnt--;
if (cnt > 0 and !path[x][y] and path[3 - x][y]) cnt--;
if (!cnt) {
cout << "Yes";
} else {
cout << "No";
}
cout << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
{ solve(); }
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int power(long long x, long long y, long long p) {
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long modi(long long a, long long m) { return power(a, m - 2, m); }
long long ncr(long long n, long long r, long long m) {
long long i, ans = 1, k = 0;
if (r == 0)
return 1;
else if (n < r)
return 0;
for (i = n; i >= n - r + 1; i--) {
ans *= i;
ans *= modi(n - i + 1, m);
ans %= m;
}
return ans;
}
bool visited[6] = {false};
int n = 6;
vector<int> adj[6];
void max_heapify(int a[100], int i) {
int l, r, largest = i;
l = 2 * i + 1;
r = 2 * i + 2;
if (l < n && a[l] > a[i]) largest = l;
if (r < n && a[r] > a[l]) largest = r;
if (largest != i) {
int k;
k = a[i];
a[i] = a[largest];
a[largest] = k;
max_heapify(a, largest);
}
}
bool square(long long n) {
double x;
x = sqrt(n);
if (x == floor(x))
return true;
else
return false;
}
bool prime(long long n) {
long long i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
void dfs(long long a, vector<long long> adj[100099], bool visited[100001],
long long dp[]) {
visited[a] = true;
if (adj[a].size() == 0) {
dp[a] = 0;
}
for (auto u : adj[a]) {
if (!visited[u]) {
visited[u] = true;
dfs(u, adj, visited, dp);
dp[a] = max(dp[a], dp[u] + 1);
} else {
dp[a] = max(dp[a], dp[u] + 1);
}
}
}
long long calhash(string& s) {
long long i, hash = 0, p = 1, pow = 3;
for (i = 0; s[i] != '\0'; i++) {
hash = (hash + p * (s[i] - 'a' + 1)) % 1000000007;
p *= pow;
}
return hash;
}
long long digit(long long n) {
long long count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
long long* kmp(string& s) {
long long j, i, prefix[1000];
prefix[0] = 0;
for (i = 1; s[i] != '\0'; i++) {
j = prefix[i - 1];
while (j > 0 && s[j] != s[i]) {
j = prefix[j - 1];
}
if (s[i] == s[j]) {
prefix[i] = j + 1;
}
}
return prefix;
}
void bfs(long long start, long long arr[], vector<long long> adj[],
long long c) {
queue<pair<long long, long long>> q;
pair<long long, long long> temp;
temp = {start, c};
q.push(temp);
while (!q.empty()) {
temp = q.front();
q.pop();
for (auto u : adj[temp.first]) {
arr[u] = arr[u] - temp.second;
q.push({u, -temp.second});
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
bool maze[4][100003];
long long n, q, i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 100003; j++) {
maze[i][j] = true;
}
}
long long blocked = 0, diag = 0;
cin >> n >> q;
while (q--) {
long long r, c;
cin >> r >> c;
if (maze[r][c])
maze[r][c] = false;
else
maze[r][c] = true;
if (!maze[r][c]) {
if (!maze[r][c] && !maze[r + 1][c] || !maze[r][c] && !maze[r - 1][c]) {
blocked++;
}
if (r == 2) {
if (!maze[r - 1][c + 1] && !maze[r - 1][c - 1])
diag += 2;
else if (!maze[r - 1][c + 1] || !maze[r - 1][c - 1])
diag++;
} else if (r == 1) {
if (!maze[r + 1][c + 1] && !maze[r + 1][c - 1])
diag += 2;
else if (!maze[r + 1][c + 1] || !maze[r + 1][c - 1])
diag++;
}
} else {
if (!maze[r + 1][c] || !maze[r - 1][c]) {
blocked--;
}
if (r == 2) {
if (!maze[r - 1][c + 1] && !maze[r - 1][c - 1]) {
diag -= 2;
} else if (!maze[r - 1][c + 1] || !maze[r - 1][c - 1])
diag--;
} else if (r == 1) {
if (!maze[r + 1][c + 1] && !maze[r + 1][c - 1])
diag -= 2;
else if (!maze[r + 1][c + 1] || !maze[r + 1][c - 1])
diag--;
}
}
if (diag >= 1 || blocked >= 1) {
cout << "No\n";
} else
cout << "Yes\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin >> n >> q;
int arr[3][n + 2];
for (int x = 0; x <= 2; x++) {
for (int y = 0; y <= n + 1; y++) {
arr[x][y] = 0;
}
}
int countB = 0;
for (int x = 0; x < q; x++) {
int a, b;
cin >> a >> b;
if (arr[a][b] == 0) {
arr[a][b] = -1;
if (a == 1) {
if (arr[a + 1][b] >= 0 && arr[a + 1][b + 1] >= 0 &&
arr[a + 1][b - 1] >= 0) {
arr[a + 1][b]++;
arr[a + 1][b + 1]++;
arr[a + 1][b - 1]++;
} else {
if (arr[a + 1][b] >= 0) {
arr[a + 1][b]++;
} else {
countB++;
}
if (arr[a + 1][b + 1] >= 0) {
arr[a + 1][b + 1]++;
} else {
countB++;
}
if (arr[a + 1][b - 1] >= 0) {
arr[a + 1][b - 1]++;
} else {
countB++;
}
}
} else {
if (arr[a - 1][b] >= 0 && arr[a - 1][b + 1] >= 0 &&
arr[a - 1][b - 1] >= 0) {
arr[a - 1][b]++;
arr[a - 1][b + 1]++;
arr[a - 1][b - 1]++;
} else {
if (arr[a - 1][b] >= 0) {
arr[a - 1][b]++;
} else {
countB++;
}
if (arr[a - 1][b + 1] >= 0) {
arr[a - 1][b + 1]++;
} else {
countB++;
}
if (arr[a - 1][b - 1] >= 0) {
arr[a - 1][b - 1]++;
} else {
countB++;
}
}
}
} else if (arr[a][b] == -1) {
arr[a][b] = 0;
if (a == 1) {
if (arr[a + 1][b] >= 0 && arr[a + 1][b - 1] >= 0 &&
arr[a + 1][b + 1] >= 0) {
arr[a + 1][b]--;
arr[a + 1][b - 1]--;
arr[a + 1][b + 1]--;
} else {
if (arr[a + 1][b] >= 0) {
arr[a + 1][b]--;
} else {
countB--;
arr[a][b]++;
}
if (arr[a + 1][b + 1] >= 0) {
arr[a + 1][b + 1]--;
} else {
countB--;
arr[a][b]++;
}
if (arr[a + 1][b - 1] >= 0) {
arr[a + 1][b - 1]--;
} else {
countB--;
arr[a][b]++;
}
}
} else {
if (arr[a - 1][b] >= 0 && arr[a - 1][b - 1] >= 0 &&
arr[a - 1][b + 1] >= 0) {
arr[a - 1][b]--;
arr[a - 1][b - 1]--;
arr[a - 1][b + 1]--;
} else {
if (arr[a - 1][b] >= 0) {
arr[a - 1][b]--;
} else {
countB--;
arr[a][b]++;
}
if (arr[a - 1][b + 1] >= 0) {
arr[a - 1][b + 1]--;
} else {
countB--;
arr[a][b]++;
}
if (arr[a - 1][b - 1] >= 0) {
arr[a - 1][b - 1]--;
} else {
countB--;
arr[a][b]++;
}
}
}
} else {
arr[a][b] = -1;
if (a == 1) {
if (arr[a + 1][b] >= 0) {
arr[a + 1][b]++;
} else {
countB++;
}
if (arr[a + 1][b + 1] >= 0) {
arr[a + 1][b + 1]++;
} else {
countB++;
}
if (arr[a + 1][b - 1] >= 0) {
arr[a + 1][b - 1]++;
} else {
countB++;
}
} else {
if (arr[a - 1][b] >= 0) {
arr[a - 1][b]++;
} else {
countB++;
}
if (arr[a - 1][b + 1] >= 0) {
arr[a - 1][b + 1]++;
} else {
countB++;
}
if (arr[a - 1][b - 1] >= 0) {
arr[a - 1][b - 1]++;
} else {
countB++;
}
}
}
if (countB == 0)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, q, bad_cells = 0;
cin >> n >> q;
bool chck[3][n + 1];
memset(chck, false, sizeof(chck));
for (int i = 0; i < q; i++) {
int row, col;
cin >> row >> col;
if (chck[row][col] == false) {
chck[row][col] = true;
for (int r = row - 1; r <= row + 1; r++) {
if (r == 0 || r == 3) continue;
if (r == row) continue;
for (int c = col - 1; c <= col + 1; c++) {
if (c == 0 || c == n + 1)
continue;
else {
if (chck[r][c] == true) {
bad_cells++;
}
}
}
}
} else {
chck[row][col] = false;
for (int r = row - 1; r <= row + 1; r++) {
if (r == 0 || r == 3) continue;
if (r == row) continue;
for (int c = col - 1; c <= col + 1; c++) {
if (c == 0 || c == n + 1)
continue;
else {
if (chck[r][c] == true) {
bad_cells--;
}
}
}
}
}
if (bad_cells > 0)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m, x, y, s;
bool f[2][N];
int main() {
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d", &x, &y);
x--;
if (f[x][y]) {
if (f[!x][y]) s--;
if (y - 1 >= 1 && f[!x][y - 1]) s--;
if (y + 1 <= n && f[!x][y + 1]) s--;
} else {
if (f[!x][y]) s++;
if (y - 1 >= 1 && f[!x][y - 1]) s++;
if (y + 1 <= n && f[!x][y + 1]) s++;
}
if (s)
puts("No");
else
puts("Yes");
f[x][y] = !f[x][y];
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
scanf("%d%d", &n, &q);
set<pair<int, int>> cells;
int bad_nei = 0;
for (int i = 0; i < q; i++) {
int row, col;
scanf("%d%d", &row, &col);
bool was_forbidden = cells.count({row, col});
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (r == row) {
continue;
}
if (!(1 <= r && r <= 2 && 1 <= c && c <= n)) {
continue;
}
if (cells.count({r, c})) {
if (was_forbidden) {
bad_nei--;
} else {
bad_nei++;
}
}
}
}
if (cells.count({row, col})) {
cells.erase({row, col});
} else {
cells.insert({row, col});
}
if (bad_nei >= 1) {
puts("NO");
} else {
puts("YES");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, q;
cin >> n >> q;
int a[2][n];
memset(a[0], 0, n * (sizeof(int)));
memset(a[1], 0, n * (sizeof(int)));
int blocked = 0;
while (q--) {
long long int x, y;
cin >> x >> y;
x--;
y--;
int delta = (a[x][y] == 0) ? -1 : +1;
a[x][y] = 1 - a[x][y];
for (int dy = -1; dy < +2; dy++) {
if (a[1 - x][y + dy] == 1 && y + dy < n && y + dy > -1) {
blocked += delta;
}
}
if (blocked == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
long long pow(long long a, long long b, long long mod) {
long long prod = 1;
long long mult = a;
while (b != 0) {
if (b % 2) {
prod *= mult;
prod %= mod;
}
mult *= mult;
mult %= mod;
b /= 2;
}
return prod;
}
long long mod_inv(long long q, long long mod) { return pow(q, mod - 2, mod); }
const long long MAXN = 1e5 + 10;
long long a[3][MAXN];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, q;
cin >> n >> q;
set<pair<pair<long long, long long>, pair<long long, long long> > > blocked;
for (long long i = 0; i < q; i++) {
long long r, c;
cin >> r >> c;
a[r][c] = 1 - a[r][c];
if (a[r][c]) {
if (a[3 - r][c - 1]) {
blocked.insert({{r, c}, {3 - r, c - 1}});
}
if (a[3 - r][c]) {
blocked.insert({{r, c}, {3 - r, c}});
}
if (a[3 - r][c + 1]) {
blocked.insert({{r, c}, {3 - r, c + 1}});
}
}
if (!a[r][c]) {
if (a[3 - r][c - 1]) {
blocked.erase({{r, c}, {3 - r, c - 1}});
blocked.erase({{3 - r, c - 1}, {r, c}});
}
if (a[3 - r][c]) {
blocked.erase({{r, c}, {3 - r, c}});
blocked.erase({{3 - r, c}, {r, c}});
}
if (a[3 - r][c + 1]) {
blocked.erase({{r, c}, {3 - r, c + 1}});
blocked.erase({{3 - r, c + 1}, {r, c}});
}
}
if (blocked.size()) {
cout << "No" << '\n';
} else
cout << "Yes" << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
int g[3][M];
int main() {
int n, q;
scanf("%d%d", &n, &q);
int cnt = 0;
int r, c;
for (int i = 0; i < q; i++) {
scanf("%d%d", &r, &c);
int ncnt = 0;
for (int i = -1; i < 2; i++) {
if (g[3 - r][c + i]) ncnt++;
}
g[r][c] ^= 1;
if (g[r][c])
cnt += ncnt;
else
cnt -= ncnt;
printf("%s\n", cnt ? "No" : "Yes");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, q;
bool a[2][100001];
int main() {
cin >> n >> q;
int cnt = 0;
while (q--) {
int x, y;
cin >> x >> y;
x--;
y--;
if (a[x][y] == 0) {
a[x][y] = 1;
for (int y1 = y - 1; y1 <= y + 1; y1++) {
if (y1 < 0 || y1 > n) continue;
if (a[1 - x][y1] == 1) cnt++;
}
} else {
a[x][y] = 0;
for (int y1 = y - 1; y1 <= y + 1; y1++) {
if (y1 < 0 || y1 > n) continue;
if (a[1 - x][y1] == 1) cnt--;
}
}
if (cnt)
cout << "no" << endl;
else
cout << "yes" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename... Args>
void logger(string vars, Args &&...values) {
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ", "));
cout << endl;
}
vector<long long> invi(long long n) {
long long x;
vector<long long> a;
for (long long i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
return a;
}
vector<bool> prime;
void checkPrime(long long n) {
for (long long i = 0; i < n + 1; i++) {
prime.push_back(true);
}
prime[0] = false;
prime[1] = false;
for (long long p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
void solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
}
void solve() {
long long n, q;
cin >> n >> q;
vector<vector<long long>> mat(3, vector<long long>(n + 2, 0));
long long block = 0;
for (long long i = 0; i < q; i++) {
long long x, y;
cin >> x >> y;
mat[x][y] = !mat[x][y];
if (x == 1) {
x = 2;
} else {
x = 1;
}
if (mat[x == 1 ? 2 : 1][y]) {
if (mat[x][y - 1]) {
block++;
}
if (mat[x][y]) {
block++;
}
if (mat[x][y + 1]) {
block++;
}
} else {
if (mat[x][y - 1]) {
block--;
}
if (mat[x][y]) {
block--;
}
if (mat[x][y + 1]) {
block--;
}
}
if (block) {
cout << "No"
<< "\n";
} else {
cout << "Yes"
<< "\n";
}
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.