답안 #1077344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077344 2024-08-27T05:37:28 Z komasan Lozinke (COCI17_lozinke) C++14
100 / 100
187 ms 23380 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int , int>
#define fi first
#define se second
#define endl '\n'
const int INF = (int)1e9;
const int maxn = 20000 + 6;
const int mod = 1e9 + 7;

int n;

string a[maxn];

const int base = 311;
const int nmod = 2;
const int ModHash[4] = {(int)1e9 + 2277 , (int)1e9 + 5277 , (int)1e9 + 8277 , (int)1e9 + 9277};

ll pw[maxn][nmod];

void prepare(int n)
{

    for (int i = 0 ; i < nmod ; ++i) pw[0][i] = 1;

    for (int i = 1 ; i <= n ; ++i)
        for (int j = 0 ; j < nmod ; ++j) pw[i][j] = pw[i - 1][j] * base % ModHash[j];

}

struct HASH{

    vector<vector<ll>> HashVal;

    void BuildHash(string s)
        {

            int n = s.size();

            HashVal.resize(n + 1);

            for (int i = 0 ; i <= n ; ++i) HashVal[i].resize(nmod);

            for (int i = 1 ; i <= n ; ++i)
                for (int j = 0 ; j < nmod ; ++j)
                    HashVal[i][j] =( HashVal[i - 1][j] * base + (int)s[i - 1]) % ModHash[j];

        }
    
    ll get(int l , int r , int pos)
        {

            return (HashVal[r][pos] - HashVal[l - 1][pos] * pw[r - l + 1][pos] % ModHash[pos] + ModHash[pos]) % ModHash[pos];

        }

} h[maxn];

map<pii , int> m;

ll ans = 0;


void calc(int id)
{
    int n = a[id].size();

    map<pii , bool> mark;

    for (int i = 1 ; i <= n ; ++i)
        for (int j = i ; j <= n ; ++j)
            {   
                if ( mark[{ h[id].get(i , j , 0) , h[id].get(i , j , 1)}] == 0)
                    {
                        ans += m[{ h[id].get(i , j , 0) , h[id].get(i , j , 1)}];
                        mark[{ h[id].get(i , j , 0) , h[id].get(i , j , 1)}] = 1;
                    }
                // if (m[{ h[id].get(i , j , 0) , h[id].get(i , j , 1)}])
                //     cout << id << " " << i << " " << j << " " << ans << endl;
            }

    m[{ h[id].get(1 , n , 0) , h[id].get(1 , n , 1) } ]++;
    // cerr <<   id << " " << m[{ h[3].get(1 , 1 , 0) , h[3].get(1 , 1 , 1) } ] << " " << m[{ h[1].get(1 , 1 , 0) , h[1].get(1 , 1 , 1) } ]<< endl;
    // cerr << a[3][0] << " " << a[1][0] << endl;

}


int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);


    prepare(15);


    cin >> n ;

    for (int i = 1 ; i <= n ; ++i) cin >> a[i];

    sort(a + 1 , a + n + 1 , [&](string a , string b)
        {
            return (a.size() != b.size() ? a.size() < b.size() : a < b);
        });

    // for (int i = 1 ; i <= n ; ++i) cout << a[i] << endl;

    for (int i = 1 ; i <= n ; ++i) h[i].BuildHash(a[i]);

    for (int i = 1 ; i <= n ; ++i) calc(i);
    
    

    for (int i = 1 ; i <= n ; ++i) 
        {
            int cnt = m[{ h[i].get(1 , a[i].size() , 0) , h[i].get(1 , a[i].size() , 1) } ];

            ans += (cnt - 1);
            m[{ h[i].get(1 , a[i].size() , 0) , h[i].get(1 , a[i].size() , 1) } ]--;

        }
    
    
    cout << ans << endl;
    
    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1372 KB Output is correct
2 Correct 1 ms 1560 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 5 ms 2140 KB Output is correct
6 Correct 9 ms 2340 KB Output is correct
7 Correct 11 ms 3164 KB Output is correct
8 Correct 17 ms 3912 KB Output is correct
9 Correct 39 ms 6716 KB Output is correct
10 Correct 83 ms 11796 KB Output is correct
11 Correct 70 ms 9904 KB Output is correct
12 Correct 184 ms 22096 KB Output is correct
13 Correct 84 ms 13172 KB Output is correct
14 Correct 156 ms 20036 KB Output is correct
15 Correct 187 ms 23380 KB Output is correct
16 Correct 68 ms 13552 KB Output is correct
17 Correct 34 ms 13916 KB Output is correct
18 Correct 31 ms 11600 KB Output is correct
19 Correct 122 ms 15948 KB Output is correct
20 Correct 47 ms 10096 KB Output is correct