結果

問題 No.430 文字列検索
ユーザー bluemeganebluemegane
提出日時 2021-05-15 17:11:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,881 bytes
コンパイル時間 4,613 ms
コンパイル使用メモリ 111,664 KB
実行使用メモリ 51,268 KB
最終ジャッジ日時 2024-04-14 07:38:51
合計ジャッジ時間 3,900 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
23,856 KB
testcase_01 AC 133 ms
51,268 KB
testcase_02 AC 69 ms
26,236 KB
testcase_03 AC 66 ms
22,196 KB
testcase_04 AC 26 ms
24,168 KB
testcase_05 AC 26 ms
22,196 KB
testcase_06 AC 27 ms
26,084 KB
testcase_07 AC 27 ms
24,040 KB
testcase_08 AC 51 ms
28,156 KB
testcase_09 AC 26 ms
24,036 KB
testcase_10 AC 29 ms
23,996 KB
testcase_11 AC 85 ms
27,732 KB
testcase_12 AC 82 ms
27,868 KB
testcase_13 AC 83 ms
27,744 KB
testcase_14 AC 81 ms
25,952 KB
testcase_15 AC 79 ms
28,156 KB
testcase_16 AC 72 ms
24,288 KB
testcase_17 AC 75 ms
24,328 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using static System.Math;
using System.Collections.Generic;
using System;

class RollingHash
{
    public const ulong B = (ulong)1e9 + 7;
    public string S { get; set; }
    public int N { get; set; }
    public ulong[] Power { get; set; }
    public ulong[] Hash { get; set; }
    public RollingHash(string s)
    {
        this.S = s;
        this.N = s.Length;
        this.Power = new ulong[this.N + 1];
        this.Power[0] = 1;
        for (int i = 0; i < N; i++) this.Power[i + 1] = this.Power[i] * B;
        this.Hash = new ulong[this.N + 1];
        for (int i = 0; i < N; i++) this.Hash[i + 1] = this.Hash[i] * B + S[i];
    }
    public ulong Get(int l, int r) => this.Hash[r] - (this.Hash[l] * this.Power[r - l]);
}

public class Hello
{
    static void Main()
    {
        var s = Console.ReadLine().Trim();
        var m = int.Parse(Console.ReadLine().Trim());
        var c = new string[m];
        var cLmax = 0;
        for (int i = 0; i < m; i++)
        {
            var c0 = Console.ReadLine().Trim();
            c[i] = c0;
            cLmax = Max(cLmax, c0.Length);
        }
        getAns(s, m, c, cLmax);
    }
    static void getAns(string s, int m, string[] c, int cLmax)
    {
        var sL = s.Length;
        var rh = new RollingHash(s);
        var d = new Dictionary<ulong, int>();
        for (int i = 0; i < sL; i++)
            for (int j = 1; j <= cLmax; j++)
            {
                if (i + j <= sL)
                {
                    var t = rh.Get(i, i + j);
                    if (d.ContainsKey(t)) d[t]++;
                    else d[t] = 1;
                }
            }
        var count = 0;
        foreach (var x in c)
        {
            var rhc = new RollingHash(x);
            var t2 = rhc.Get(0, x.Length);
            if (d.ContainsKey(t2)) count += d[t2];
        }
        Console.WriteLine(count);
    }
}
0