結果
| 問題 | 
                            No.515 典型LCP
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-05-06 00:45:19 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,739 bytes | 
| コンパイル時間 | 1,349 ms | 
| コンパイル使用メモリ | 115,748 KB | 
| 実行使用メモリ | 134,540 KB | 
| 最終ジャッジ日時 | 2024-09-14 09:46:39 | 
| 合計ジャッジ時間 | 15,672 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 12 TLE * 3 | 
ソースコード
#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];
            }
        }
    }
    const vector<long long>& getHash(int a) const{
        return hash[a+1];
    }
    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(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;
}