Submission #165742

# Submission time Handle Problem Language Result Execution time Memory
165742 2019-11-28T12:31:51 Z Sensei Lozinke (COCI17_lozinke) C++17
100 / 100
480 ms 42560 KB
#include <bits/stdc++.h>

using namespace std;

const long long P = 61;
const long long MOD = 1e16 + 3;

const int MAXN = 2e4;

long long add (long long x, long long y) {
	return ((x % MOD) + (y % MOD)) % MOD;
}

long long mul (long long x, long long y) {
	return ((x % MOD) * (y % MOD)) % MOD;
}

// vector<char> st[MAXN + 7];
string st[MAXN + 7];
long long fullhash[MAXN + 7];
// vector<long long> subhashes[MAXN + 7];
unordered_set<long long> subhashes[MAXN + 7];
char buff[12];
map<long long, int> fullhashes;

int main () {
	int N;
	cin >> N;

	for (int i = 1; i <= N; i++) {
		// scanf("%s", buff);

		// for (int j = 0; buff[j] != 0; j++) {
		// 	st[i].push_back(buff[j]);
		// }

		cin >> st[i];
	}

	for (int i = 1; i <= N; i++) {
		long long hash = 0;

		for (int j = 0; j < st[i].size(); j++) {
			hash = mul(hash, P);
			hash = add(hash, st[i][j] - 'a' + 1);
		}

		fullhash[i] = hash;
		fullhashes[hash]++;
	}

	long long ans = 0;

	for (int i = 1; i <= N; i++) {
		for (int ss = 0; ss < st[i].size(); ss++) {
			long long hash = 0;

			for (int j = ss; j < st[i].size(); j++) {
				hash = mul(hash, P);
				hash = add(hash, st[i][j] - 'a' + 1);

				// subhashes[i].push_back(hash);

				if (subhashes[i].find(hash) == subhashes[i].end()) {
					subhashes[i].insert(hash);
					ans += fullhashes[hash];
				}
			}
		}

		// sort(subhashes[i].begin(), subhashes[i].end());
	}

	ans -= N;

	// for (int i = 1; i <= N; i++) {
	// 	for (long long hash : subhashes[i]) {
	// 		ans += fullhashes.count(hash);
	// 	}

	// 	ans--;
	// }

	cout << ans << "\n";

	return 0;
}

Compilation message

lozinke.cpp: In function 'int main()':
lozinke.cpp:43:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < st[i].size(); j++) {
                   ~~^~~~~~~~~~~~~~
lozinke.cpp:55:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int ss = 0; ss < st[i].size(); ss++) {
                    ~~~^~~~~~~~~~~~~~
lozinke.cpp:58:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j = ss; j < st[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2040 KB Output is correct
2 Correct 4 ms 2040 KB Output is correct
3 Correct 4 ms 2172 KB Output is correct
4 Correct 5 ms 2168 KB Output is correct
5 Correct 11 ms 3292 KB Output is correct
6 Correct 16 ms 3960 KB Output is correct
7 Correct 22 ms 4728 KB Output is correct
8 Correct 36 ms 6392 KB Output is correct
9 Correct 80 ms 12024 KB Output is correct
10 Correct 192 ms 21496 KB Output is correct
11 Correct 134 ms 17884 KB Output is correct
12 Correct 461 ms 40748 KB Output is correct
13 Correct 222 ms 29944 KB Output is correct
14 Correct 335 ms 31864 KB Output is correct
15 Correct 480 ms 42560 KB Output is correct
16 Correct 215 ms 32348 KB Output is correct
17 Correct 73 ms 11388 KB Output is correct
18 Correct 60 ms 9692 KB Output is correct
19 Correct 278 ms 30328 KB Output is correct
20 Correct 116 ms 18680 KB Output is correct