結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
iwkjosec
|
| 提出日時 | 2019-10-03 13:26:22 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,509 bytes |
| コンパイル時間 | 898 ms |
| コンパイル使用メモリ | 105,344 KB |
| 実行使用メモリ | 22,912 KB |
| 最終ジャッジ日時 | 2024-11-10 00:35:22 |
| 合計ジャッジ時間 | 4,097 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | AC * 1 TLE * 1 -- * 12 |
コンパイルメッセージ
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 a = 0;
for (int i = 0; i < m; i++)
{
var c = ReadLine();
var h = RollingHash.ComputeHash(c, 27);
for (int j = 0; j <= s.Length - c.Length; j++)
{
var r = rh.Hash(j, j + c.Length);
if (r == h) a++;
}
}
WriteLine(a);
}
}
class RollingHash
{
ulong[] hash;
ulong[] pow;
ulong @base;
const ulong mod = 998244353;
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;
}
}
iwkjosec