結果

問題 No.515 典型LCP
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2017-05-05 22:52:33
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 10,874 bytes
コンパイル時間 3,038 ms
コンパイル使用メモリ 110,076 KB
実行使用メモリ 133,760 KB
最終ジャッジ日時 2023-10-12 10:01:17
合計ジャッジ時間 15,782 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 AC 642 ms
127,732 KB
testcase_03 AC 51 ms
20,460 KB
testcase_04 AC 51 ms
20,508 KB
testcase_05 WA -
testcase_06 AC 657 ms
117,636 KB
testcase_07 AC 645 ms
119,620 KB
testcase_08 WA -
testcase_09 AC 604 ms
133,676 KB
testcase_10 AC 593 ms
131,732 KB
testcase_11 AC 598 ms
131,704 KB
testcase_12 AC 588 ms
131,716 KB
testcase_13 AC 605 ms
133,760 KB
testcase_14 AC 404 ms
131,772 KB
testcase_15 AC 618 ms
118,044 KB
testcase_16 AC 621 ms
120,144 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.Linq;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using Number = System.Int64;
namespace Program
{
    public class Solver
    {
        public void Solve()
        {
            var n = sc.Integer();
            var sb = new StringBuilder();
            var pos = new int[n];
            var len = new int[n];
            for (int i = 0; i < n; i++)
            {
                pos[i] = sb.Length;
                sb.Append(sc.Scan());
                len[i] = sb.Length - pos[i];
            }
            var sa = new SuffixArray(sb.ToString());
            sa.Init();
            sa.BuildLCP();
            var m = sc.Long();
            var x = sc.Long();
            var d = sc.Long();
            long mod = n;
            mod *= n - 1;
            var ans = 0L;
            for (int 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 lcp = sa.GetLCP(pos[i], pos[j]);
                lcp = Math.Min(lcp, len[i]);
                lcp = Math.Min(lcp, len[j]);
                ans += lcp;
                x = (x + d) % mod;

            }
            IO.Printer.Out.WriteLine(ans);
        }

        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
    }
}
#region main
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer: StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion
#region SuffixArray
/// <summary>
/// O(N) 構築
/// </summary>
/// 
public class SuffixArray
{
    int[] S;
    int N;
    int[] SA;
    int[] rank;
    sparseTableRMQ rmq;
    const char ZERO = 'a';
    const int K = 26;
    public SuffixArray(string str)
    {
        N = str.Length;
        S = new int[N + 3];
        for (int i = 0; i < N; i++) S[i] = str[i] - ZERO + 1;
        SA = new int[N];
        rank = new int[N + 1];
    }
    public void Init()
    {
        rec(S, SA, N, K);
        for (int i = 0; i < N; i++)
            rank[SA[i]] = i + 1;
        rank[N] = 0;
    }
    public void BuildLCP()
    {
        var k = 0;
        var h = new int[N + 1];
        for (int i = 0; i < N; i++)
        {
            var j = this[rank[i] - 1];
            if (k > 0) k--;
            for (; j + k < N && i + k < N; k++) if (S[j + k] != S[i + k]) break;
            h[rank[i] - 1] = k;
        }
        rmq = new sparseTableRMQ(h);
    }
    public int GetLCP(int i, int j)
    {
        i = rank[i]; j = rank[j];
        return rmq.Query(Math.Min(i, j), Math.Max(i, j));
    }
    void rec(int[] s, int[] sa, int n, int k)
    {
        //Debug.WriteLine("{0} {1}", n, k);
        if (n == 1) { sa[0] = 0; return; }
        int a = (n + 2) / 3, b = (n + 1) / 3, c = n / 3;
        int ac = a + c;

        int[] tbc = new int[ac + 3], tabc = new int[ac + 3];
        int[] ta = new int[a];
        int[] taa = new int[a];

        for (int i = 0, j = 0; i < n + (a - c); i++) if (i % 3 != 0) tbc[j++] = i;
        radixPass(tbc, tabc, s, 2, ac, k);
        radixPass(tabc, tbc, s, 1, ac, k);
        radixPass(tbc, tabc, s, 0, ac, k);

        int name = 0, c0 = -1, c1 = -1, c2 = -1;
        for (int i = 0; i < ac; i++)
        {
            if (s[tabc[i]] != c0 || s[tabc[i] + 1] != c1 || s[tabc[i] + 2] != c2)
            {
                name++; c0 = s[tabc[i]]; c1 = s[tabc[i] + 1]; c2 = s[tabc[i] + 2];
            }
            if (tabc[i] % 3 == 1) tbc[tabc[i] / 3] = name;
            else
            {

                tbc[tabc[i] / 3 + a] = name;
            }
        }
        if (name < ac)
        {
            tbc[ac] = 0;
            rec(tbc, tabc, ac, name);
            for (int i = 0; i < ac; i++) tbc[tabc[i]] = i + 1;
        }
        else
        {
            for (int i = 0; i < ac; i++) tabc[tbc[i] - 1] = i;
        }
        for (int i = 0, j = 0; i < ac; i++) if (tabc[i] < a) ta[j++] = 3 * tabc[i];
        radixPass(ta, taa, s, 0, a, k);
        for (int p = 0, t = a - b, ptr = 0; ptr < n; ptr++)
        {
            int i = (tabc[t] < a) ? (tabc[t] * 3 + 1) : ((tabc[t] - a) * 3 + 2);
            var j = taa[p];
            if ((tabc[t] < a) ?
                s[i] < s[j] || (s[i] == s[j] && tbc[tabc[t] + a] < tbc[j / 3]) :
                s[i] < s[j] ||
                (s[i] == s[j] && s[i + 1] < s[j + 1]) ||
                (s[i] == s[j] && s[i + 1] == s[j + 1] && tbc[tabc[t] - a + 1] < tbc[j / 3 + a]))
            {
                sa[ptr] = i; t++;
                if (t == ac) for (ptr++; p < a; p++, ptr++) sa[ptr] = taa[p];
            }
            else
            {
                sa[ptr] = j; p++;
                if (p == a)
                    for (ptr++; t < ac; t++, ptr++) sa[ptr] = (tabc[t] < a) ? (tabc[t] * 3 + 1) : ((tabc[t] - a) * 3 + 2);

            }
        }
    }
    void radixPass(int[] a, int[] b, int[] s, int offset, int n, int k)
    {
        var c = new int[k + 1];
        for (int i = 0; i < n; i++) c[s[a[i] + offset]]++;
        for (int i = 0, sum = 0; i <= k; i++)
        {
            var t = c[i]; c[i] = sum; sum += t;
        }
        var ret = new int[n];
        for (int i = 0; i < n; i++) b[c[s[a[i] + offset]]++] = a[i];
    }
    /// <summary>
    /// rankがiのものを返す
    /// </summary>
    public int this[int index]
    {
        get { return index == 0 ? N : SA[index - 1]; }
    }
    /// <summary>
    /// s[i:]のランクを返す
    /// </summary>
    public int Rank(int index)
    {
        return rank[index];
    }
    #region SparseTableRMQ
    public class sparseTableRMQ
    {
        int n;
        int[] A;
        public sparseTableRMQ(int[] a)
        {
            var k = 1;
            n = a.Length;
            for (int i = 1; i < n; i <<= 1) k++;

            A = new int[n * k];
            for (int i = 0; i < n; i++)
                A[i] = a[i];
            var d = 0;
            for (int i = 1; i < n; i <<= 1)
            {
                for (int j = 0; j < n; j++)
                    A[d + n + j] = A[d + j];
                d += n;
                for (int j = 0; j < n - i; j++)
                    A[d + j] = Math.Min(A[d + j], A[d + j + i]);
            }
        }
        /// <summary>
        /// value of [l,r)
        /// </summary>
        public int Query(int l, int r)
        {
            r--;
            int z = r - l, k = 0, e = 1, s;
            s = ((z & 0xffff0000) != 0 ? 1 : 0) << 4; z >>= s; e <<= s; k |= s;
            s = ((z & 0x0000ff00) != 0 ? 1 : 0) << 3; z >>= s; e <<= s; k |= s;
            s = ((z & 0x000000f0) != 0 ? 1 : 0) << 2; z >>= s; e <<= s; k |= s;
            s = ((z & 0x0000000c) != 0 ? 1 : 0) << 1; z >>= s; e <<= s; k |= s;
            s = ((z & 0x00000002) != 0 ? 1 : 0) << 0; z >>= s; e <<= s; k |= s;
            //var vl = A[l + n * k];
            //var vr = A[r - e + 1 + (n * k)];
            return Math.Min(A[l + (n * k)], A[r + (n * k) - e + 1]);
        }
    }
    #endregion
}
#endregion
0