#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;
};
struct Node {
bool terminal;
ll have;
vector<Node*> nxt;
Node(bool t) {
nxt.resize(27, nullptr);
terminal = t;
have = terminal;
}
};
ll get(Node *t) {
if (!t) {
return 0;
}
return t->have;
}
void recalc(Node *t) {
if (!t) {
return;
}
t->have = t->terminal;
for (int i = 0; i < 27; i++) {
t->have += get(t->nxt[i]);
}
}
void add(Node *t, string &s, int i) {
if (i == s.size()) {
t->terminal = true;
t->have++;
return;
}
if (t->nxt[s[i] - 'a'] == nullptr) {
t->nxt[s[i] - 'a'] = new Node(false);
}
add(t->nxt[s[i] - 'a'], s, i + 1);
recalc(t);
}
ll get_bad(Node *t, string &s, int i) {
if (!t or i == s.size()) {
return 0;
}
if (s[i] == '{') {
ll ans = 0;
for (int j = 0; j < 27; j++) {
ans += get_bad(t->nxt[j], s, i + 1);
}
return ans;
} else {
ll ans = 0;
for (int j = 0; j < 26; j++) {
if (s[i] - 'a' == j) {
continue;
}
ans += get(t->nxt[j]);
}
ans += get_bad(t->nxt[s[i] - 'a'], s, i + 1);
ans += get_bad(t->nxt[26], s, i + 1);
return ans;
}
}
void solve() {
Node *cnt = new Node(false);
ll n, m;
cin >> n >> m;
ll answer = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (auto &c : s) {
if (c == '?') {
c = '{';
}
}
answer += i - get_bad(cnt, s, 0);
add(cnt, s, 0);
}
cout << answer;
}
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;
}
Compilation message
parametriziran.cpp: In function 'void add(Node*, std::__cxx11::string&, int)':
parametriziran.cpp:68:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i == s.size()) {
~~^~~~~~~~~~~
parametriziran.cpp: In function 'long long int get_bad(Node*, std::__cxx11::string&, int)':
parametriziran.cpp:82:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (!t or i == s.size()) {
~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
640 KB |
Output is correct |
2 |
Correct |
30 ms |
612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
700 KB |
Output is correct |
2 |
Correct |
24 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
3900 KB |
Output is correct |
2 |
Correct |
462 ms |
1604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
652 ms |
3096 KB |
Output is correct |
2 |
Correct |
1479 ms |
4532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
875 ms |
16348 KB |
Output is correct |
2 |
Correct |
2960 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3049 ms |
17956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
671 ms |
26372 KB |
Output is correct |
2 |
Execution timed out |
3050 ms |
4980 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
965 ms |
24896 KB |
Output is correct |
2 |
Execution timed out |
3023 ms |
2420 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3046 ms |
33536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |