結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  norioc | 
| 提出日時 | 2016-10-03 20:43:04 | 
| 言語 | C#(csc) (csc 3.9.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 40 ms / 2,000 ms | 
| コード長 | 1,678 bytes | 
| コンパイル時間 | 879 ms | 
| コンパイル使用メモリ | 107,520 KB | 
| 実行使用メモリ | 20,480 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:08:26 | 
| 合計ジャッジ時間 | 1,761 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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);
    }
}
            
            
            
        