結果
| 問題 |
No.515 典型LCP
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-01-24 17:27:53 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 289 ms / 1,000 ms |
| コード長 | 2,065 bytes |
| コンパイル時間 | 958 ms |
| コンパイル使用メモリ | 110,464 KB |
| 実行使用メモリ | 22,656 KB |
| 最終ジャッジ日時 | 2024-06-12 23:37:48 |
| 合計ジャッジ時間 | 3,517 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 15 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
T[] readArray(T)(size_t n){auto a=new T[](n),r=readln.splitter;foreach(ref v;a){v=r.front.to!T;r.popFront;}return a;}
T[] readArrayM(T)(size_t n){auto a=new T[](n);foreach(ref v;a)v=readln.chomp.to!T;return a;}
void main()
{
int n; readV(n);
auto s = readArrayM!string(n);
long m, x, d; readV(m, x, d);
struct SS { string s; int i; }
auto ss = new SS[](n);
foreach (i; 0..n) ss[i] = SS(s[i], i+1);
ss.sort!"a.s < b.s";
auto cv = new int[](n+1);
foreach (i; 0..n) cv[ss[i].i] = i;
auto t = new int[](n-1);
foreach (i; 0..n-1) t[i] = lcp(ss[i].s, ss[i+1].s);
auto st = SparseTable!int(t), ans = 0L;
foreach (k; 1..m+1) {
auto i = (x/(n-1))+1;
auto j = (x%(n-1))+1;
if (i > j) swap(i, j);
else ++j;
x = (x+d)%(n.to!long*(n-1));
auto i2 = cv[i], j2 = cv[j];
if (i2 > j2) swap(i2, j2);
ans += st[i2..j2];
}
writeln(ans);
}
auto lcp(string a, string b)
{
auto n = min(a.length, b.length).to!int;
foreach (i; 0..n)
if (a[i] != b[i]) return i;
return n;
}
struct SparseTable(T, alias pred = "a < b ? a : b")
{
import std.algorithm, std.functional;
alias predFun = binaryFun!pred;
size_t[] logTable;
size_t[][] rmq;
size_t n;
T[] a;
this(T[] a)
{
this.a = a;
this.n = a.length;
logTable = new size_t[n+1];
foreach (i; 2..n+1)
logTable[i] = logTable[i>>1]+1;
rmq = new size_t[][](logTable[n]+1, n);
foreach (i; 0..n) rmq[0][i] = i;
for (size_t k = 1; (1<<k) < n; ++k)
for (size_t i = 0; i+(1<<k) <= n; ++i) {
auto x = rmq[k-1][i];
auto y = rmq[k-1][i+(1<<k-1)];
rmq[k][i] = predFun(a[x], a[y]) == a[x] ? x : y;
}
}
pure T opSlice(size_t l, size_t r)
{
auto k = logTable[r-l-1];
auto x = rmq[k][l];
auto y = rmq[k][r-(1<<k)];
return predFun(a[x], a[y]);
}
pure size_t opDollar() { return n; }
}