結果

問題 No.430 文字列検索
ユーザー iwkjoseciwkjosec
提出日時 2019-10-03 14:00:43
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,450 bytes
コンパイル時間 899 ms
コンパイル使用メモリ 114,920 KB
実行使用メモリ 53,628 KB
最終ジャッジ日時 2024-04-14 09:08:14
合計ジャッジ時間 2,975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
23,952 KB
testcase_01 AC 128 ms
50,488 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 26 ms
23,436 KB
testcase_05 AC 25 ms
23,700 KB
testcase_06 AC 26 ms
23,676 KB
testcase_07 AC 27 ms
25,988 KB
testcase_08 AC 108 ms
53,628 KB
testcase_09 AC 27 ms
25,992 KB
testcase_10 AC 34 ms
28,280 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 = 0L;
        for (int i = 0; i < m; i++)
        {
            var c = ReadLine();
            var h = RollingHash.ComputeHash(c, 27);
            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
{
    ulong[] hash;
    ulong[] pow;
    ulong @base;
    const ulong mod = (2 << 61) - 1;

    public RollingHash(string s, int b = 27)
    {
        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] = pow[i - 1] * (ulong)b % mod;
            hash[i] = hash[i - 1] * (ulong)b % mod + s[i - 1] % mod;
        }
        @base = (ulong)b;
    }

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

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