結果

問題 No.430 文字列検索
ユーザー iwkjoseciwkjosec
提出日時 2019-10-04 12:11:13
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 3,324 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 114,392 KB
実行使用メモリ 55,828 KB
最終ジャッジ日時 2024-04-14 09:50:54
合計ジャッジ時間 2,933 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
25,488 KB
testcase_01 AC 126 ms
55,828 KB
testcase_02 AC 64 ms
25,564 KB
testcase_03 AC 64 ms
25,692 KB
testcase_04 AC 30 ms
25,648 KB
testcase_05 AC 30 ms
25,364 KB
testcase_06 AC 31 ms
27,688 KB
testcase_07 AC 30 ms
23,476 KB
testcase_08 AC 121 ms
52,580 KB
testcase_09 AC 31 ms
27,940 KB
testcase_10 AC 37 ms
28,048 KB
testcase_11 AC 75 ms
31,116 KB
testcase_12 AC 73 ms
29,108 KB
testcase_13 AC 72 ms
29,332 KB
testcase_14 AC 71 ms
25,276 KB
testcase_15 AC 72 ms
25,264 KB
testcase_16 AC 64 ms
25,708 KB
testcase_17 AC 65 ms
25,960 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();
        var m = int.Parse(ReadLine());
        var rh = new RollingHash(s);
        var d = new Map<ulong, int>();
        for (int i = 1; i < 11; i++)
        {
            for (int j = 0; j <= s.Length - i; j++)
            {
                d[rh.Hash(j, j + i)]++;
            }
        }
        var a = 0;
        for (int i = 0; i < m; i++)
        {
            var c = ReadLine();
            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;
    }
}

class RollingHash
{
    const ulong MASK30 = (1UL << 30) - 1;
    const ulong MASK31 = (1UL << 31) - 1;
    const ulong MOD = (1UL << 61) - 1;
    const ulong POSITIVIZER = MOD * ((1UL << 3) - 1);
    static uint Base;
    static ulong[] powMemo = new ulong[100000 + 1];
    static RollingHash()
    {
        Base = (uint)new Random().Next(129, int.MaxValue);
        powMemo[0] = 1;
        for (int i = 1; i < powMemo.Length; i++)
            powMemo[i] = CalcMod(Mul(powMemo[i - 1], Base));
    }

    ulong[] hash;

    public RollingHash(string s)
    {
        hash = new ulong[s.Length + 1];
        for (int i = 0; i < s.Length; i++)
            hash[i + 1] = CalcMod(Mul(hash[i], Base) + s[i]);
    }

    public ulong Hash(int begin, int end)
    {
        return CalcMod(hash[end] + POSITIVIZER - Mul(hash[begin], powMemo[end - begin]));
    }

    public static ulong ComputeHash(string s)
    {
        var h = 0UL;
        for (int i = 0; i < s.Length; i++)
        {
            h = CalcMod(Mul(h, Base)+s[i]);
        }
        return h;
    }
    
    private static ulong Mul(ulong l, ulong r)
    {
        var lu = l >> 31;
        var ld = l & MASK31;
        var ru = r >> 31;
        var rd = r & MASK31;
        var middleBit = ld * ru + lu * rd;
        return ((lu * ru) << 1) + ld * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30);
    }

    private static ulong Mul(ulong l, uint r)
    {
        var lu = l >> 31;
        var rd = r & MASK31;
        var middleBit = lu * rd;
        return (l & MASK31) * rd + ((middleBit & MASK30) << 31) + (middleBit >> 30);
    }

    private static ulong CalcMod(ulong val)
    {
        val = (val & MOD) + (val >> 61);
        if (val > MOD) val -= MOD;
        return val;
    }
}
0