답안 #403244

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
403244 2021-05-13T00:13:32 Z penguinhacker Lozinke (COCI17_lozinke) C++14
100 / 100
131 ms 2652 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const unsigned long long SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(SEED);
const int M=1e9+7, B1=31, B2=53;

int n;
ll ans;
map<string, int> oc[11];
map<ar<int, 2>, int> mp;

void add(ar<int, 2>& a, char c) {
	a[0]=((ll)B1*a[0]+c)%M;
	a[1]=((ll)B2*a[1]+c)%M;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=0; i<n; ++i) {
		string s;
		cin >> s;
		++oc[s.size()][s];
	}
	for (int i=1; i<=10; ++i) {
		for (auto& x : oc[i]) {
			set<ar<int, 2>> st;
			string s=x.first;
			ll cnt=x.second;
			ans+=(ll)cnt*(cnt-1);
			for (int i=0; i<s.size(); ++i) {
				ar<int, 2> hsh={};
				for (int j=i; j<s.size(); ++j) {
					add(hsh, s[j]);
					st.insert(hsh);
				}
			}
			for (const ar<int, 2>& j : st)
				if (mp.count(j))
					ans+=cnt*mp[j];
			ar<int, 2> hsh={};
			for (char c : s)
				add(hsh, c);
			mp[hsh]+=cnt;
		}
	}
	cout << ans;
	return 0;
}

Compilation message

lozinke.cpp: In function 'int main()':
lozinke.cpp:36:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |    for (int i=0; i<s.size(); ++i) {
      |                  ~^~~~~~~~~
lozinke.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int j=i; j<s.size(); ++j) {
      |                   ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 5 ms 332 KB Output is correct
6 Correct 8 ms 460 KB Output is correct
7 Correct 12 ms 516 KB Output is correct
8 Correct 9 ms 460 KB Output is correct
9 Correct 48 ms 1092 KB Output is correct
10 Correct 48 ms 1348 KB Output is correct
11 Correct 80 ms 1612 KB Output is correct
12 Correct 87 ms 1672 KB Output is correct
13 Correct 101 ms 2020 KB Output is correct
14 Correct 131 ms 2652 KB Output is correct
15 Correct 98 ms 2100 KB Output is correct
16 Correct 16 ms 460 KB Output is correct
17 Correct 3 ms 204 KB Output is correct
18 Correct 3 ms 204 KB Output is correct
19 Correct 89 ms 1988 KB Output is correct
20 Correct 19 ms 528 KB Output is correct