結果

問題 No.2761 Substitute and Search
ユーザー 👑 kakel-sankakel-san
提出日時 2024-05-17 22:40:08
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 3,821 ms / 4,000 ms
コード長 5,166 bytes
コンパイル時間 11,396 ms
コンパイル使用メモリ 167,736 KB
実行使用メモリ 386,708 KB
最終ジャッジ日時 2024-05-17 22:40:56
合計ジャッジ時間 41,275 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
30,848 KB
testcase_01 AC 61 ms
30,964 KB
testcase_02 AC 62 ms
30,848 KB
testcase_03 AC 62 ms
30,972 KB
testcase_04 AC 3,821 ms
252,896 KB
testcase_05 AC 2,956 ms
196,960 KB
testcase_06 AC 817 ms
191,484 KB
testcase_07 AC 2,731 ms
229,848 KB
testcase_08 AC 854 ms
192,196 KB
testcase_09 AC 2,806 ms
229,564 KB
testcase_10 AC 858 ms
192,760 KB
testcase_11 AC 2,887 ms
229,328 KB
testcase_12 AC 1,994 ms
228,840 KB
testcase_13 AC 1,968 ms
228,976 KB
testcase_14 AC 1,981 ms
229,488 KB
testcase_15 AC 1,992 ms
386,708 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (105 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/

ソースコード

diff #

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);
        }
    }
}
0