結果
問題 | No.2761 Substitute and Search |
ユーザー | tenten |
提出日時 | 2024-05-23 15:56:10 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 2,149 ms / 4,000 ms |
コード長 | 4,154 bytes |
コンパイル時間 | 2,652 ms |
コンパイル使用メモリ | 79,348 KB |
実行使用メモリ | 176,076 KB |
最終ジャッジ日時 | 2024-05-23 15:56:37 |
合計ジャッジ時間 | 22,749 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
50,464 KB |
testcase_01 | AC | 52 ms
50,184 KB |
testcase_02 | AC | 52 ms
50,432 KB |
testcase_03 | AC | 53 ms
50,188 KB |
testcase_04 | AC | 1,457 ms
165,568 KB |
testcase_05 | AC | 1,981 ms
130,316 KB |
testcase_06 | AC | 851 ms
128,964 KB |
testcase_07 | AC | 1,640 ms
161,404 KB |
testcase_08 | AC | 1,138 ms
140,340 KB |
testcase_09 | AC | 1,670 ms
161,604 KB |
testcase_10 | AC | 862 ms
129,088 KB |
testcase_11 | AC | 2,149 ms
176,076 KB |
testcase_12 | AC | 1,338 ms
142,624 KB |
testcase_13 | AC | 1,344 ms
142,080 KB |
testcase_14 | AC | 1,307 ms
141,456 KB |
testcase_15 | AC | 1,355 ms
142,224 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static final int BASE = 343; static final int MOD = 1000000009; static long[] values; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int length = sc.nextInt(); int q = sc.nextInt(); values = new long[length]; values[0] = 60; for (int i = 1; i < length; i++) { values[i] = values[i - 1] * BASE % MOD; } char[][] inputs = new char[n][]; SegmentTree[] sts = new SegmentTree[n]; for (int i = 0; i < n; i++) { inputs[i] = sc.next().toCharArray(); sts[i] = new SegmentTree(length); for (int j = 0; j < length; j++) { sts[i].add(j, (inputs[i][j] - 'a' + 1) * values[j] % MOD); } } StringBuilder sb = new StringBuilder(); while (q-- > 0) { if (sc.nextInt() == 1) { int idx = sc.nextInt() - 1; char left = sc.next().charAt(0); char right = sc.next().charAt(0); for (int i = 0; i < n; i++) { if (inputs[i][idx] == left) { inputs[i][idx] = right; sts[i].add(idx, (right - left + MOD) % MOD * values[idx] % MOD); } } } else { long ans = 0; char[] current = sc.next().toCharArray(); for (int i = 0; i < current.length; i++) { ans += (current[i] - 'a' + 1) * values[i] % MOD; ans %= MOD; } int count = 0; for (int i = 0; i < n; i++) { if (sts[i].getValue(current.length) == ans) { count++; } } sb.append(count).append("\n"); } } System.out.print(sb); } static class SegmentTree { int size; long[] values; public SegmentTree(int x) { size = 2; while (size < x) { size <<= 1; } values = new long[size * 2 - 1]; } public void add(int idx, long v) { int current = idx + size - 1; values[current] += v; values[current] %= MOD; calc((current - 1) / 2); } private void calc(int idx) { values[idx] = (values[idx * 2 + 1] + values[idx * 2 + 2]) % MOD; if (idx > 0) { calc((idx - 1) / 2); } } public long getValue(int right) { return getValue(0, 0, size, right); } private long getValue(int idx, int min, int max, int right) { if (max <= right) { return values[idx]; } else if (min >= right) { return 0; } else { return (getValue(idx * 2 + 1, min, (min + max) / 2, right) + getValue(idx * 2 + 2, (min + max) / 2, max, right)) % MOD; } } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }