結果
| 問題 | No.958 たぷりすたべる (回文) |
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2019-12-21 00:11:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 65 ms / 2,000 ms |
| コード長 | 1,893 bytes |
| 記録 | |
| コンパイル時間 | 1,593 ms |
| コンパイル使用メモリ | 170,120 KB |
| 実行使用メモリ | 6,632 KB |
| 最終ジャッジ日時 | 2024-07-18 04:55:18 |
| 合計ジャッジ時間 | 5,200 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 53 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
// Manacher's Algorithm: radius of palindromes
// Input: std::string of length N
// Output: std::vector<int> of size N
// Complexity: O(N)
// Sample:
// - `sakanakanandaka` -> [1, 1, 2, 1, 4, 1, 4, 1, 2, 2, 1, 1, 1, 2, 1]
// Reference: <https://snuke.hatenablog.com/entry/2014/12/02/235837>
std::vector<int> manacher(std::string S)
{
std::vector<int> res(S.length());
int i = 0, j = 0;
while (i < (int)S.size()) {
while (i - j >= 0 and i + j < (int)S.size() and S[i - j] == S[i + j]) j++;
res[i] = j;
int k = 1;
while (i - k >= 0 and i + k < (int)S.size() and k + res[i - k] < j) res[i + k] = res[i - k], k++;
i += k, j -= k;
}
return res;
}
int main()
{
lint N, K, Q;
cin >> N >> K >> Q;
string S;
cin >> S;
if (K <= 5)
{
string T;
while (K--) T += S;
vector<int> ret = manacher(T);
while (Q--)
{
lint a;
cin >> a;
printf("%d\n", ret[a - 1] * 2 - 1);
}
return 0;
}
vector<int> man3 = manacher(S + S + S);
lint LEN = K * N;
while (Q--)
{
lint a;
cin >> a;
a--;
lint ret;
if (a < N)
{
ret = man3[a];
}
else if (a >= LEN - N)
{
ret = man3[a % N + N * 2];
}
else
{
ret = man3[a % N + N];
if (ret >= N)
{
ret = a + 1;
mmin(ret, LEN - a);
}
}
printf("%lld\n", ret * 2 - 1);
}
}
hitonanode