結果
| 問題 |
No.2761 Substitute and Search
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-17 22:40:08 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 3,970 ms / 4,000 ms |
| コード長 | 5,166 bytes |
| コンパイル時間 | 16,192 ms |
| コンパイル使用メモリ | 165,936 KB |
| 実行使用メモリ | 390,660 KB |
| 最終ジャッジ日時 | 2024-12-20 14:42:29 |
| 合計ジャッジ時間 | 43,330 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (99 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
static int NN => int.Parse(ReadLine());
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = NList;
var (n, l, q) = (c[0], c[1], c[2]);
var s = SList(n);
var query = SList(q);
var hlist1 = new StringHash[n];
var hlist2 = new StringHash[n];
for (var i = 0; i < n; ++i)
{
hlist1[i] = new StringHash(StringHashInitializer.H1, s[i]);
hlist2[i] = new StringHash(StringHashInitializer.H2, s[i]);
}
var sc = new char[n][];
for (var i = 0; i < n; ++i) sc[i] = s[i].ToCharArray();
var ans = new List<int>();
for (var i = 0; i < q; ++i)
{
if (query[i][0] == '1')
{
var que = query[i].Split();
var k = int.Parse(que[1]) - 1;
for (var j = 0; j < n; ++j)
{
if (sc[j][k] == que[2][0])
{
sc[j][k] = que[3][0];
hlist1[j].Change(k, que[3][0]);
hlist2[j].Change(k, que[3][0]);
}
}
}
else
{
var a = 0;
var t = query[i].Split()[1];
var h1 = new StringHash(StringHashInitializer.H1, t).GetHash(0, t.Length - 1);
var h2 = new StringHash(StringHashInitializer.H2, t).GetHash(0, t.Length - 1);
for (var j = 0; j < n; ++j)
{
if (h1 == hlist1[j].GetHash(0, t.Length - 1) && h2 == hlist2[j].GetHash(0, t.Length - 1)) ++a;
}
ans.Add(a);
}
}
WriteLine(string.Join("\n", ans));
}
static long Exp(long n, long p, int mod)
{
long _n = n % mod;
var _p = p;
var result = 1L;
if ((_p & 1) == 1) result *= _n;
while (_p > 0)
{
_n = _n * _n % mod;
_p >>= 1;
if ((_p & 1) == 1) result = result * _n % mod;
}
return result;
}
public class StringHashInitializer
{
public static StringHashInitializer H1 = new StringHashInitializer(29, 998_244_391, 722_866_628);
public static StringHashInitializer H2 = new StringHashInitializer(31, 998_244_397, 193_208_593);
public int Mul;
public int Mod;
public int Rev;
public StringHashInitializer(int mul, int mod, int rev)
{
Mul = mul; Mod = mod; Rev = rev;
}
}
class StringHash
{
int mul;
int mod;
long[] pow;
long[] rev;
// ここをFenwicktree等に変更することで、文字列の変更ができる
FenwickTree cum;
public StringHash(StringHashInitializer ini, string s)
{
mul = ini.Mul;
mod = ini.Mod;
pow = new long[s.Length + 1];
rev = new long[s.Length + 1];
pow[0] = 1;
rev[0] = 1;
cum = new FenwickTree(s.Length + 1);
for (var i = 1; i <= s.Length; ++i)
{
pow[i] = pow[i - 1] * mul % mod;
rev[i] = rev[i - 1] * ini.Rev % mod;
cum.Add(i, (s[i - 1] - 'a' + 1) * pow[i] % mod);
}
}
public void Change(int pos, char c)
{
cum.Add(pos + 1, (c - 'a' + 1) * pow[pos + 1] % mod - cum.Get(pos + 1));
}
/// <summary>部分文字列s[l..r]のハッシュ1を求める l,rは0-based</summary>
public int GetHash(int l, int r)
{
if (l > r) return 0;
return (int)((cum.Sum(r + 1) - cum.Sum(l) + mod) % mod * rev[l] % mod);
}
}
class FenwickTree
{
int size;
long[] tree;
public FenwickTree(int size)
{
this.size = size;
tree = new long[size + 2];
}
public void Add(int index, long value)
{
++index;
for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
}
/// <summary>先頭からindexまでの和(include index)</summary>
public long Sum(int index)
{
if (index < 0) return 0;
++index;
var sum = 0L;
for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
return sum;
}
public long Get(int index)
{
if (index == 0) return Sum(0);
return Sum(index) - Sum(index - 1);
}
}
}