結果
問題 |
No.430 文字列検索
|
ユーザー |
|
提出日時 | 2017-10-27 17:32:54 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 75 ms / 2,000 ms |
コード長 | 1,145 bytes |
コンパイル時間 | 2,462 ms |
コンパイル使用メモリ | 104,576 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-10 00:18:16 |
合計ジャッジ時間 | 3,828 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto s = readln.chomp, n = s.length; auto sa = SuffixArray(s); auto saa = new string[](n); foreach (i; 0..n) saa[i] = sa[i]; auto sas = saa.assumeSorted; auto m = readln.chomp.to!size_t, ans = size_t(0); foreach (_; 0..m) { auto c = readln.chomp; auto r1 = sas.lowerBound(c); auto r2 = sas.upperBound(c ~ "|"); ans += n - r1.length - r2.length; } writeln(ans); } struct SuffixArray { import std.algorithm; string s; size_t n; size_t[] x; this(string s) { this.s = s; n = s.length; x = new size_t[](n); auto r = new size_t[](n), t = new size_t[](n); foreach (i; 0..n) r[x[i] = i] = s[i]; for (size_t h = 1; t[n-1] != n-1; h <<= 1) { auto cmp(size_t i, size_t j) { if (r[i] != r[j]) return r[i] < r[j]; return i+h < n && j+h < n ? r[i+h] < r[j+h] : i > j; } x.sort!((a, b) => cmp(a, b)); foreach (i; 0..n-1) t[i+1] = t[i] + cmp(x[i], x[i+1]); foreach (i; 0..n) r[x[i]] = t[i]; } } auto opIndex(size_t i) { return s[x[i]..$]; } }