Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <climits>
- #include <cassert>
- #include <unordered_map>
- using namespace std;
- int trie[1000000][12];
- int64_t countWords[1000000];
- int tFree = 2;
- int modA = 1000000007;
- int modB = 1000000009;
- unordered_map<int64_t, int64_t> sufMap;
- int main() {
- int n;
- cin >> n;
- string s[n];
- for (int it = 0; it < n; ++it) {
- cin >> s[it];
- int cur = 1;
- for (int i = 0; i < s[it].size(); ++i) {
- int c = s[it][i] - 'a';
- if (!trie[cur][c]) {
- trie[cur][c] = tFree++;
- }
- cur = trie[cur][c];
- }
- countWords[cur]++;
- }
- int64_t ans = 0;
- for (int it = 0; it < n; ++it) {
- int counts[s[it].size() + 1];
- int cur = 1;
- for (int i = 0; i < s[it].size(); ++i) {
- int c = s[it][i] - 'a';
- if (!trie[cur][c]) {
- trie[cur][c] = tFree++;
- }
- cur = trie[cur][c];
- counts[i] = countWords[cur];
- }
- counts[s[it].size() - 1] = 0;
- int64_t hash = 0;
- for (int i = s[it].size() - 1; i >= 0; --i) {
- if (counts[i]) {
- //cerr << s[it] << " " << i << " " << counts[i] << endl;
- ans += sufMap[hash] * counts[i];
- sufMap[hash] += counts[i];
- }
- hash = 47 * hash + s[it][i];
- }
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement