#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
#ifdef LOCAL
#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int B = 78623;
const int MOD = 1e9 + 7;
const int N = 20020;
string s[N];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
for (int i = 0; i < n; i++)
cin >> s[i];
sort(s, s + n, [&](string a, string b) {
if (a.size() != b.size())
return a.size() < b.size();
return a < b;
});
map<int, int> cnt;
ll ans = 0;
for (int i = 0; i < n; i++) {
int m = (int)s[i].size();
ll myHash = 0;
set<ll> st;
for (int l = 0; l < m; l++) {
ll hsh = 0;
for (int r = l; r < m; r++) {
hsh = hsh * B + (s[i][r] - 'a' + 1);
hsh %= MOD;
st.insert(hsh);
}
if (l == 0)
myHash = hsh;
}
for (auto p : st)
ans += cnt[p];
cnt[myHash]++;
}
for (auto& p : cnt) {
ans += (ll)p.second * (p.second - 1) / 2;
}
printf("%lld", ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
5 ms |
1108 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
11 ms |
1492 KB |
Output is correct |
8 |
Correct |
16 ms |
2028 KB |
Output is correct |
9 |
Correct |
37 ms |
1928 KB |
Output is correct |
10 |
Correct |
87 ms |
4940 KB |
Output is correct |
11 |
Correct |
64 ms |
2892 KB |
Output is correct |
12 |
Correct |
195 ms |
10336 KB |
Output is correct |
13 |
Correct |
95 ms |
1976 KB |
Output is correct |
14 |
Incorrect |
143 ms |
9284 KB |
Output isn't correct |
15 |
Incorrect |
235 ms |
10124 KB |
Output isn't correct |
16 |
Correct |
67 ms |
1056 KB |
Output is correct |
17 |
Correct |
24 ms |
948 KB |
Output is correct |
18 |
Correct |
20 ms |
852 KB |
Output is correct |
19 |
Correct |
117 ms |
5424 KB |
Output is correct |
20 |
Correct |
45 ms |
1012 KB |
Output is correct |