結果

問題 No.430 文字列検索
ユーザー noriocnorioc
提出日時 2016-10-03 20:43:04
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,678 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 112,476 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2024-05-03 04:29:56
合計ジャッジ時間 2,147 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
19,712 KB
testcase_01 AC 49 ms
20,096 KB
testcase_02 AC 30 ms
20,352 KB
testcase_03 AC 31 ms
20,480 KB
testcase_04 AC 27 ms
19,840 KB
testcase_05 AC 26 ms
19,712 KB
testcase_06 AC 27 ms
19,712 KB
testcase_07 AC 27 ms
20,096 KB
testcase_08 AC 30 ms
20,176 KB
testcase_09 AC 27 ms
20,224 KB
testcase_10 AC 28 ms
19,712 KB
testcase_11 AC 34 ms
20,440 KB
testcase_12 AC 32 ms
20,456 KB
testcase_13 AC 31 ms
20,224 KB
testcase_14 AC 31 ms
20,456 KB
testcase_15 AC 32 ms
20,096 KB
testcase_16 AC 31 ms
20,480 KB
testcase_17 AC 31 ms
20,096 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;

class Program {
    static int ReadInt() { return int.Parse(Console.ReadLine()); }
    static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return Console.ReadLine().Split(); }

    private const long B = 26;

    static long Hash(string s) {
        long h = 0;
        for (int i = 0; i < s.Length; i++) {
            h = h * B + (s[i] - 'A' + 1);
        }
        return h;
    }

    static int Calc(string s, List<string> ss) {
        var hs = new HashSet<long>();
        var lens = new bool[11];
        for (int i = 0; i < ss.Count; i++) {
            lens[ss[i].Length] = true;
            hs.Add(Hash(ss[i]));
        }

        int ans = 0;
        for (int i = 1; i < 11; i++) {
            if (!lens[i]) continue; // 長さ i の検索文字列はない
            if (s.Length < i) break;

            long pow = (long)Math.Pow(26, i - 1);

            long h = 0;
            for (int j = 0; j < i; j++) {
                h = h * B + (s[j] - 'A' + 1);
            }

            if (hs.Contains(h)) ans++;
            for (int j = i; j < s.Length; j++) {
                h -= (s[j - i] - 'A' + 1) * pow;
                h = h * B + (s[j] - 'A' + 1);

                if (hs.Contains(h)) ans++;
            }
        }
        return ans;
    }

    static void Main() {
        var s = Console.ReadLine();
        int m = ReadInt();
        var ss = new List<string>();
        for (int i = 0; i < m; i++)
            ss.Add(Console.ReadLine());

        var ans = Calc(s, ss);
        Console.WriteLine(ans);
    }
}
0