# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
403236 | penguinhacker | Lozinke (COCI17_lozinke) | C++14 | 111 ms | 2500 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
#define debug(x) cerr << "[" << #x << "] = [" << x << "]\n"
template<class T> ostream& operator<< (ostream& out, vector<T> v) {
out << '[';
for (int i = 0; i < v.size(); ++i) {
if (i > 0) {
out << ", ";
}
out << v[i];
}
return out << ']';
}
template<class T> ostream& operator<< (ostream& out, set<T> v) {
return out << vector<T>(v.begin(), v.end());
}
template<class A, unsigned int sz> ostream& operator<< (ostream& out, ar<A, sz> a) {
out << '[';
for (int i = 0; i < sz; ++i) {
if (i > 0) out << ", ";
out << a[i];
}
return out << ']';
}
template<class A, class B> ostream& operator<< (ostream& out, pair<A, B> p) {
return out << '[' << p.first << ", " << p.second << ']';
}
template<class A, class B> ostream& operator<< (ostream& out, map<A, B> mp) {
return out << vector<pair<A, B>>(mp.begin(), mp.end());
}
const int M=1e9+7, B=31;
int n;
ll ans;
map<string, int> oc[11];
map<int, int> mp;
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<int> st;
string s=x.first;
int cnt=x.second;
ans+=(ll)cnt*(cnt-1);
//cout << s << " " << cnt << endl;
//debug(s);
for (int i=0; i<s.size(); ++i) {
ll hsh=0;
for (int j=i; j<s.size(); ++j) {
hsh=(B*hsh+s[j])%M;
st.insert(hsh);
}
}
//debug(mp);
//debug(st);
for (const int& j : st) {
if (mp.count(j)) {
ans+=(ll)cnt*mp[j];
//cout << s << " " << cnt << " " << mp[j] << endl;
}
}
//debug(ans);
ll hsh=0;
for (char c : s)
hsh=(B*hsh+c)%M;
mp[hsh]+=cnt;
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |