結果

問題 No.430 文字列検索
ユーザー iwkjoseciwkjosec
提出日時 2019-10-03 15:43:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 3,683 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 119,032 KB
実行使用メモリ 38,208 KB
最終ジャッジ日時 2024-04-14 09:26:59
合計ジャッジ時間 3,020 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,172 KB
testcase_01 AC 102 ms
38,208 KB
testcase_02 AC 75 ms
25,940 KB
testcase_03 AC 74 ms
23,860 KB
testcase_04 AC 32 ms
24,916 KB
testcase_05 AC 31 ms
25,176 KB
testcase_06 AC 32 ms
25,428 KB
testcase_07 AC 31 ms
24,916 KB
testcase_08 AC 95 ms
38,204 KB
testcase_09 AC 32 ms
27,260 KB
testcase_10 AC 38 ms
23,472 KB
testcase_11 AC 87 ms
31,176 KB
testcase_12 AC 84 ms
29,440 KB
testcase_13 AC 83 ms
31,016 KB
testcase_14 AC 82 ms
28,796 KB
testcase_15 AC 80 ms
28,720 KB
testcase_16 AC 77 ms
27,208 KB
testcase_17 AC 77 ms
26,284 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.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        var s = ReadLine().Select(x => x - 'A' + 1).ToArray();
        var m = int.Parse(ReadLine());
        var rh = new RollingHash(s);
        var d = new Map<ulong, int>(s.Length * 10);
        for (int i = 1; i < 11; i++)
        {
            for (int j = 0; j <= s.Length - i; j++)
            {
                d[rh.Hash(j, j + i)]++;
            }
        }
        var a = 0L;
        for (int i = 0; i < m; i++)
        {
            var c = ReadLine().Select(x => x - 'A' + 1).ToArray();
            var h = RollingHash.ComputeHash(c);
            a += d[h];
        }
        WriteLine(a);
    }
}

public class Map<TKey, TValue> : Dictionary<TKey, TValue>
{
    public Map() { }
    public Map(int capacity) : base(capacity) { }
    public Map(IEqualityComparer<TKey> comparer) : base(comparer) { }
    public Map(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
    public Map(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
    public Map(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }
    public Map(IEnumerable<KeyValuePair<TKey, TValue>> collection) : base(collection) { }
    public Map(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) : base(collection, comparer) { }

    public new TValue this[TKey key]
    {
        get => TryGetValue(key, out var value) ? value : default;
        set => base[key] = value;
    }
}

public class RollingHash
{
    ulong[] hash;
    ulong[] pow;
    const ulong @base = 27;
    const ulong mod = (1UL << 61) - 1;
    const ulong MASK30 = (1UL << 30) - 1;
    const ulong MASK31 = (1UL << 31) - 1;
    const ulong MOD = (1UL << 61) - 1;

    static ulong Mul(ulong a, ulong b)
    {
        var au = a >> 31;
        var ad = a & MASK31;
        var bu = b >> 31;
        var bd = b & MASK31;
        var mid = ad * bu + au * bd;
        var midu = mid >> 30;
        var midd = (mid & MASK30);
        return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    static ulong CalcMod(ulong val)
    {
        val = (val & MOD) + (val >> 61);
        if (val > MOD) val -= MOD;
        return val;
    }
    public RollingHash(string s)
    {
        hash = new ulong[s.Length + 1];
        pow = new ulong[s.Length + 1];
        pow[0] = 1;
        for (int i = 1; i < pow.Length; i++)
        {
            pow[i] = Mul(pow[i - 1], @base);
            hash[i] = (Mul(hash[i - 1], @base % mod) + s[i - 1]) % mod;
        }
    }

    public RollingHash(int[] s)
    {
        hash = new ulong[s.Length + 1];
        pow = new ulong[s.Length + 1];
        pow[0] = 1;
        for (int i = 1; i < pow.Length; i++)
        {
            pow[i] = Mul(pow[i - 1], @base);
            hash[i] = (Mul(hash[i - 1], @base) + (ulong)s[i - 1]) % mod;
        }
    }

    public static ulong ComputeHash(string s)
    {
        var h = 0UL;
        for (int i = 0; i < s.Length; i++)
        {
            h = (h + s[i]) % mod;
            if (i != s.Length - 1) h = Mul(h, @base);
        }
        return h;
    }


    public static ulong ComputeHash(int[] s)
    {
        var h = 0UL;
        for (int i = 0; i < s.Length; i++)
        {
            h = (h + (ulong)s[i]) % mod;
            if (i != s.Length - 1) h = Mul(h, @base);
        }
        return h;
    }

    public ulong Hash(int l, int r)
    {
        var h = 0UL;
        h = (hash[r] - Mul(hash[l], pow[r - l])) % mod;
        return h;
    }
}
0