#include <bits/stdc++.h>
using namespace std;
const int nx=2e4+5, mod=1e9+9;
#define ll long long
ll n, c[nx][12], sz[nx], res, p[nx], vl[nx];
string s;
map<ll, ll> mp;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
p[0]=1;
for (int i=1; i<=10; i++) p[i]=(p[i-1]*31)%mod;
cin>>n;
for (int i=1; i<=n; i++)
{
cin>>s;
sz[i]=s.size();
for (int j=1; j<=sz[i]; j++) c[i][j]=s[j-1]-'a'+1;
}
for (int i=1; i<=n; i++)
{
set<ll> s;
for (int j=1; j<=sz[i]; j++)
{
ll tmp=0;
for (int k=j; k<=sz[i]; k++)
{
tmp=(tmp+c[i][k]*p[k-j])%mod;
s.insert(tmp);
if (j==1&&k==sz[i]) vl[i]=tmp;
}
}
for (auto x:s) if (mp.find(x)!=mp.end()) res+=mp[x];
mp[vl[i]]++;
}
mp.clear();
for (int i=n; i>=1; i--)
{
set<ll> s;
for (int j=1; j<=sz[i]; j++)
{
ll tmp=0;
for (int k=j; k<=sz[i]; k++)
{
tmp=(tmp+c[i][k]*p[k-j])%mod;
s.insert(tmp);
}
}
for (auto x:s) if (mp.find(x)!=mp.end()) res+=mp[x];
mp[vl[i]]++;
}
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
6 ms |
2392 KB |
Output is correct |
6 |
Correct |
9 ms |
2396 KB |
Output is correct |
7 |
Correct |
12 ms |
2548 KB |
Output is correct |
8 |
Correct |
12 ms |
2396 KB |
Output is correct |
9 |
Correct |
65 ms |
2996 KB |
Output is correct |
10 |
Correct |
67 ms |
3076 KB |
Output is correct |
11 |
Correct |
99 ms |
3296 KB |
Output is correct |
12 |
Incorrect |
128 ms |
3364 KB |
Output isn't correct |
13 |
Correct |
135 ms |
3764 KB |
Output is correct |
14 |
Incorrect |
144 ms |
3920 KB |
Output isn't correct |
15 |
Incorrect |
141 ms |
3664 KB |
Output isn't correct |
16 |
Correct |
124 ms |
2892 KB |
Output is correct |
17 |
Correct |
30 ms |
3180 KB |
Output is correct |
18 |
Correct |
23 ms |
2908 KB |
Output is correct |
19 |
Incorrect |
138 ms |
3692 KB |
Output isn't correct |
20 |
Correct |
89 ms |
3040 KB |
Output is correct |