結果

問題 No.515 典型LCP
ユーザー くれちーくれちー
提出日時 2017-07-27 16:22:44
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 4,115 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 110,592 KB
実行使用メモリ 30,848 KB
最終ジャッジ日時 2024-04-18 08:52:09
合計ジャッジ時間 6,207 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 249 ms
25,088 KB
testcase_03 AC 38 ms
20,224 KB
testcase_04 AC 37 ms
20,224 KB
testcase_05 AC 187 ms
24,448 KB
testcase_06 AC 212 ms
24,448 KB
testcase_07 AC 198 ms
24,576 KB
testcase_08 AC 231 ms
24,576 KB
testcase_09 AC 220 ms
24,576 KB
testcase_10 AC 215 ms
24,704 KB
testcase_11 AC 222 ms
24,832 KB
testcase_12 AC 213 ms
24,704 KB
testcase_13 AC 195 ms
24,448 KB
testcase_14 AC 67 ms
24,960 KB
testcase_15 AC 185 ms
24,704 KB
testcase_16 AC 186 ms
24,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Extentions;

class IO
{
    int idx;
    string[] input;

    public IO(TextReader reader)
    {
        input = reader.ReadToEnd().Split(new[] { " ", "\n", "\r" },
            StringSplitOptions.RemoveEmptyEntries);
    }

    T Get<T>(Func<string, T> parser) => parser(input[idx++]);

    public string S => Get(s => s);
    public char C => Get(char.Parse);
    public int I => Get(int.Parse);
    public long L => Get(long.Parse);
    public double F => Get(double.Parse);
    public decimal D => Get(decimal.Parse);
    public BigInteger B => Get(BigInteger.Parse);

    T[] Gets<T>(int n, Func<string, T> parser)
        => input.Skip((idx += n) - n).Take(n).Select(parser).ToArray();

    public string[] Ss(int n) => Gets(n, s => s);
    public char[] Cs(int n) => Gets(n, char.Parse);
    public int[] Is(int n) => Gets(n, int.Parse);
    public long[] Ls(int n) => Gets(n, long.Parse);
    public double[] Fs(int n) => Gets(n, double.Parse);
    public decimal[] Ds(int n) => Gets(n, decimal.Parse);
    public BigInteger[] Bs(int n) => Gets(n, BigInteger.Parse);

    public void Write<T>(params T[] xs) => WriteLine(string.Join(" ", xs));
    public void Write(params object[] xs) => WriteLine(string.Join(" ", xs));
}

class SparseTable
{
    int[] xs, logTable;
    int[,] table;
    int n;

    public SparseTable(int[] xs)
    {
        n = xs.Length;
        this.xs = xs;

        logTable = new int[n + 1];
        for (var i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1;

        table = new int[n, logTable[n] + 1];
        for (var i = 0; i < n; i++) table[i, 0] = i;
        for (var k = 1; (1 << k) < n; k++)
        {
            for (var i = 0; i + (1 << k) <= n; i++)
            {
                var a = table[i, k - 1];
                var b = table[i + (1 << (k - 1)), k - 1];
                table[i, k] = xs[a] < xs[b] ? a : b;
            }
        }
    }

    public int Query(int i, int j)
    {
        var k = logTable[j - i];
        return Min(xs[table[i, k]], xs[table[j - (1 << k) + 1, k]]);
    }
}

static class Extentions
{
    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x;
        x = y;
        y = tmp;
    }
}

static class Program
{
    public static void Main()
    {
        var sw = new StreamWriter(OpenStandardOutput()) { NewLine = "\n" };
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        SetOut(sw);
        Solve(new IO(In));
        Out.Flush();
    }

    static int Lcp(string a, string b)
    {
        var cnt = 0;
        var len = Min(a.Length, b.Length);
        for (var i = 0; i < len; i++) if (a[i] == b[i]) cnt++; else break;
        return cnt;
    }

    static void Solve(IO io)
    {
        var n = io.I;
        var s = io.Ss(n);
        var m = io.I;
        var x = io.L;
        var d = io.L;

        if (n > 10000) return; // for debug
        
        var bsi = new int[n];
        var asi = new int[n];
        for (var i = 0; i < n; i++) bsi[i] = asi[i] = i;
        Array.Sort(bsi, (a, b) => s[a].CompareTo(s[b]));
        Array.Sort(asi, (a, b) => bsi[a].CompareTo(bsi[b]));

        var ts = new string[n];
        for (var i = 0; i < n; i++) ts[i] = s[bsi[i]];
        s = ts;

        var lcplen = new int[n - 1];
        for (var i = 0; i < n - 1; i++) lcplen[i] = Lcp(s[i], s[i + 1]);

        var st = new SparseTable(lcplen);
        var ans = 0L;
        var mod = (long)n * (n - 1);

        for (var k = 0; k < m; k++)
        {
            var i = x / (n - 1);
            var j = x % (n - 1);
            if (i > j) Swap(ref i, ref j);
            else j++;

            var p = asi[i];
            var q = asi[j];
            if (p > q) Swap(ref p, ref q);
            var t = st.Query(p, q - 1);
            ans += t;

            x = (x + d) % mod;
        }

        io.Write(ans);
    }
}
0