#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod1 = (ll)1e9 + 7;
const ll mod2 = (ll)1e9 + 9;
const ll BASE = 539;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
void solve() {
ll n, k;
cin >> n >> k;
vector<string> arr(n);
cin >> arr;
vector<ll> p(27);
p[0] = 1;
for (int i = 1; i < 27; i++) {
p[i] = (p[i - 1] * BASE) % mod1;
}
vector<ll> h(n);
vector<vector<ll>> pref(n, vector<ll>(n + 1)), suff(n, vector<ll>(n + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
pref[i][j + 1] = (pref[i][j] * BASE + arr[i][j] - 'a' + 1) % mod1;
suff[i][n - j - 1] = (suff[i][n - j] * BASE + arr[i][n - j - 1] - 'a' + 1) % mod1;
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
h[j] += p[arr[j][i] - 'a'];
if (h[j] >= mod1) {
h[j] -= mod1;
}
}
}
for (int i = k - 1; i < n; i++) {
set<vector<ll>> used;
for (int t = 0; t < n; t++) {
vector<ll> cnt = {pref[t][i - k + 1], h[t], suff[t][i + 1]};
if (used.count(cnt) != 0) {
cout << "DA";
return;
}
used.insert(cnt);
}
if (i + 1 < n) {
for (int j = 0; j < n; j++) {
h[j] -= p[arr[j][i - k + 1] - 'a'];
if (h[j] < 0) {
h[j] += mod1;
}
h[j] += p[arr[j][i + 1] - 'a'];
if (h[j] >= mod1) {
h[j] -= mod1;
}
}
}
}
cout << "NE";
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
9 ms |
1024 KB |
Output is correct |
4 |
Correct |
9 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
3 |
Correct |
7 ms |
1024 KB |
Output is correct |
4 |
Correct |
4 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
996 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
4 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
18 ms |
1124 KB |
Output is correct |
3 |
Correct |
8 ms |
1100 KB |
Output is correct |
4 |
Correct |
6 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4608 KB |
Output is correct |
2 |
Correct |
10 ms |
4608 KB |
Output is correct |
3 |
Correct |
16 ms |
4668 KB |
Output is correct |
4 |
Correct |
10 ms |
4864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
4608 KB |
Output is correct |
2 |
Correct |
8 ms |
4608 KB |
Output is correct |
3 |
Correct |
67 ms |
4608 KB |
Output is correct |
4 |
Correct |
23 ms |
4864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4608 KB |
Output is correct |
2 |
Correct |
11 ms |
4608 KB |
Output is correct |
3 |
Correct |
41 ms |
4608 KB |
Output is correct |
4 |
Correct |
11 ms |
4864 KB |
Output is correct |