結果

問題 No.515 典型LCP
ユーザー mamekinmamekin
提出日時 2017-05-06 00:59:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,167 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 116,212 KB
実行使用メモリ 134,872 KB
最終ジャッジ日時 2023-10-12 10:57:44
合計ジャッジ時間 11,325 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 808 ms
134,668 KB
testcase_01 TLE -
testcase_02 AC 901 ms
115,224 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 458 ms
115,424 KB
testcase_06 AC 532 ms
115,376 KB
testcase_07 AC 434 ms
115,416 KB
testcase_08 AC 552 ms
115,224 KB
testcase_09 AC 692 ms
115,644 KB
testcase_10 AC 408 ms
115,828 KB
testcase_11 AC 399 ms
115,996 KB
testcase_12 AC 398 ms
115,652 KB
testcase_13 AC 530 ms
115,440 KB
testcase_14 AC 171 ms
92,316 KB
testcase_15 AC 426 ms
114,912 KB
testcase_16 AC 437 ms
114,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

/*
class RollingHash
{
private:
    static const int X[], M[];
    vector<vector<long long> > xp;
    vector<vector<long long> > hash;
public:
    RollingHash(const string& s){
        int n = s.size();
        xp.assign(n+1, vector<long long>(3, 1));
        hash.assign(n+1, vector<long long>(3, 0));
        for(int i=0; i<n; ++i){
            for(int j=0; j<3; ++j){
                hash[i+1][j] = hash[i][j];
                hash[i+1][j] *= X[j];
                hash[i+1][j] += s[i];
                hash[i+1][j] %= M[j];
                xp[i+1][j] = xp[i][j];
                xp[i+1][j] *= X[j];
                xp[i+1][j] %= M[j];
            }
        }
    }
    tuple<long long, long long, long long> getHash(int a) const{
        return make_tuple(hash[a+1][0], hash[a+1][1], hash[a+1][2]);
    }
    tuple<long long, long long, long long> getHash(int a, int b) const{
        vector<long long> ans(3);
        for(int i=0; i<3; ++i){
            ans[i] = hash[b+1][i];
            ans[i] -= hash[a][i] * xp[b-a+1][i];
            ans[i] %= M[i];
            if(ans[i] < 0)
                ans[i] += M[i];
        }
        return make_tuple(ans[0], ans[1], ans[2]);
    }
};
const int RollingHash::X[] = {1685440109, 1389328937, 1813126193}; // 素数
const int RollingHash::M[] = {2000000087, 2000000089, 2000000099}; // 合同式の法(素数)
*/

class RollingHash
{
private:
    static const int X[], M[];
    vector<vector<long long> > xp;
    vector<vector<long long> > hash;
public:
    RollingHash(const string& s){
        int n = s.size();
        xp.assign(n+1, vector<long long>(2, 1));
        hash.assign(n+1, vector<long long>(2, 0));
        for(int i=0; i<n; ++i){
            for(int j=0; j<2; ++j){
                hash[i+1][j] = hash[i][j];
                hash[i+1][j] *= X[j];
                hash[i+1][j] += s[i];
                hash[i+1][j] %= M[j];
                xp[i+1][j] = xp[i][j];
                xp[i+1][j] *= X[j];
                xp[i+1][j] %= M[j];
            }
        }
    }
    pair<long long, long long> getHash(int a) const{
        return make_pair(hash[a+1][0], hash[a+1][1]);
    }
    pair<long long, long long> getHash(int a, int b) const{
        vector<long long> ans(2);
        for(int i=0; i<2; ++i){
            ans[i] = hash[b+1][i];
            ans[i] -= hash[a][i] * xp[b-a+1][i];
            ans[i] %= M[i];
            if(ans[i] < 0)
                ans[i] += M[i];
        }
        return make_pair(ans[0], ans[1]);
    }
};
const int RollingHash::X[] = {1685440109, 1389328937, 1813126193}; // 素数
const int RollingHash::M[] = {2000000087, 2000000089, 2000000099}; // 合同式の法(素数)

int solve(int len, const RollingHash& a, const RollingHash& b)
{
    int left = 0;
    int right = len;
    while(left < right){
        int mid = (left + right + 1) / 2;
        if(a.getHash(mid-1) != b.getHash(mid-1))
            right = mid - 1;
        else
            left = mid;
    }
    return left;
}

int main()
{
    int n;
    cin >> n;
    vector<string> s(n);
    for(int i=0; i<n; ++i)
        cin >> s[i];

    vector<RollingHash> rh;
    for(int i=0; i<n; ++i)
        rh.push_back(RollingHash(s[i]));

    int m;
    long long x, d;
    cin >> m >> x >> d;
    vector<int> a(m), b(m);
    for(int i=0; i<m; ++i){
        a[i] = (int)(x / (n - 1));
        b[i] = (int)(x % (n - 1));
        if(a[i] > b[i])
            swap(a[i], b[i]);
        else
            ++ b[i];
        x = (x + d) % (n * (n - 1LL));
    }

    long long ans = 0;
    for(int i=0; i<m; ++i){
        int len = min(s[a[i]].size(), s[b[i]].size());
        ans += solve(len, rh[a[i]], rh[b[i]]);
    }
    cout << ans << endl;

    return 0;
}
0