結果

問題 No.515 典型LCP
ユーザー mamekinmamekin
提出日時 2017-05-05 22:59:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,907 bytes
コンパイル時間 1,645 ms
コンパイル使用メモリ 123,124 KB
実行使用メモリ 179,760 KB
最終ジャッジ日時 2023-10-12 10:06:44
合計ジャッジ時間 6,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
            }
        }
    }
    vector<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 ans;
    }
};
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(0, mid-1) != b.getHash(0, 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;
    map<pair<int, int>, int> memo;
    for(int i=0; i<m; ++i){
        pair<int, int> key(a[i], b[i]);
        if(memo.find(key) != memo.end()){
            ans += memo[key];
        }
        else{
            int len = min(s[a[i]].size(), s[b[i]].size());
            int tmp = solve(len, rh[a[i]], rh[b[i]]);
            ans += tmp;
            memo[key] = tmp;
        }
    }
    cout << ans << endl;

    return 0;
}
0