結果
問題 | No.430 文字列検索 |
ユーザー | iwkjosec |
提出日時 | 2019-10-04 17:03:05 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 98 ms / 2,000 ms |
コード長 | 3,060 bytes |
コンパイル時間 | 780 ms |
コンパイル使用メモリ | 115,544 KB |
実行使用メモリ | 42,312 KB |
最終ジャッジ日時 | 2024-11-10 00:35:37 |
合計ジャッジ時間 | 2,259 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 20 ms
17,792 KB |
testcase_01 | AC | 98 ms
42,312 KB |
testcase_02 | AC | 57 ms
18,560 KB |
testcase_03 | AC | 56 ms
18,176 KB |
testcase_04 | AC | 22 ms
18,048 KB |
testcase_05 | AC | 21 ms
17,792 KB |
testcase_06 | AC | 22 ms
17,920 KB |
testcase_07 | AC | 22 ms
17,920 KB |
testcase_08 | AC | 96 ms
41,940 KB |
testcase_09 | AC | 22 ms
17,920 KB |
testcase_10 | AC | 28 ms
20,352 KB |
testcase_11 | AC | 64 ms
21,888 KB |
testcase_12 | AC | 64 ms
22,144 KB |
testcase_13 | AC | 63 ms
22,016 KB |
testcase_14 | AC | 62 ms
20,224 KB |
testcase_15 | AC | 61 ms
20,608 KB |
testcase_16 | AC | 57 ms
18,304 KB |
testcase_17 | AC | 57 ms
18,432 KB |
コンパイルメッセージ
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; 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; } } public class RollingHash { ulong[] hash; ulong[] pow; public int Length => hash.Length - 1; const ulong @base = 131; const ulong mod = (1UL << 61) - 1; public RollingHash(string s) { hash = new ulong[s.Length + 1]; pow = new ulong[s.Length + 1]; pow[0] = 1; for (int i = 1; i < pow.Length; i++) { hash[i] = (Mul(hash[i - 1], @base) + s[i - 1]) % mod; pow[i] = Mul(pow[i - 1], @base); } } public ulong this[int i] { get { return hash[i]; } } public ulong Hash(int l, int r) { var a = hash[r]; var b = Mul(hash[l], pow[r - l]); if (a < b) a += mod; return (a - b) % mod; } public static ulong ComputeHash(string s) { var h = 0UL; foreach (var i in s) { h = Mul(h, @base) + i; } return h % mod; } static ulong Mul(ulong a, ulong b) { const ulong MASK30 = (1UL << 30) - 1; const ulong MASK31 = (1UL << 31) - 1; var ah = a >> 31; var al = a & MASK31; var bh = b >> 31; var bl = b & MASK31; var m = al * bh + ah * bl; var mh = m >> 30; var ml = m & MASK30; return Mod(ah * bh * 2 + mh + (ml << 31) + al * bl); } static ulong Mod(ulong v) { v = (v & mod) + (v >> 61); if (v > mod) v -= mod; return v; } }