QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#220406 | #5166. 回文匹配 | hos_lyric# | 0 | 134ms | 9528kb | C++14 | 5.5kb | 2023-10-20 10:42:37 | 2024-07-04 02:20:47 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
const int n = as.size();
vector<int> rs(2 * n + 1);
for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
for (; 0 < i - j && i + j < 2 * n &&
(!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
++j) {}
rs[i] = j;
for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
}
return rs;
}
char buf[500'010];
int T, N, Q;
vector<string> S;
vector<int> I, J;
namespace brute {
vector<int> run() {
cerr<<"[brute::run]"<<endl;
vector<vector<int>> rss(N + 1);
for (int i = 1; i <= N; ++i) {
rss[i] = manacher(S[i]);
// cerr<<S[i]<<": "<<rss[i]<<endl;
}
vector<vector<int>> f(N + 1, vector<int>(N + 1, 0));
for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) {
const int lenI = S[i].size();
const int lenJ = S[j].size();
for (int l = 0; l <= lenJ - lenI; ++l) {
bool ok = true;
for (int x = 0; x <= 2 * lenI; ++x) {
ok = ok && (rss[i][x] == min({rss[j][2 * l + x], x, 2 * lenI - x}));
}
if (ok) {
++f[i][j];
}
}
}
vector<int> ans(Q, 0);
for (int q = 0; q < Q; ++q) {
ans[q] = f[I[q]][J[q]];
}
return ans;
}
} // brute
namespace sub2 {
vector<int> run() {
cerr<<"[sub2::run]"<<endl;
vector<pair<vector<int>, int>> ps(N + 1);
for (int i = 1; i <= N; ++i) {
ps[i] = make_pair(manacher(S[i]), i);
}
sort(ps.begin() + 1, ps.end());
int id = 0;
vector<int> ids(N + 1, -1);
for (int i = 1, j = 1; i <= N; i = j) {
for (; j <= N && ps[i].first == ps[j].first; ++j) {
ids[ps[j].second] = id;
}
++id;
}
vector<int> ans(Q, 0);
for (int q = 0; q < Q; ++q) {
ans[q] = (ids[I[q]] == ids[J[q]]) ? 1 : 0;
}
return ans;
}
} // sub2
namespace slow {
int solve(const string &s, const string &t) {
const int sLen = s.size();
const int tLen = t.size();
const auto rsS = manacher(s);
const auto rsT = manacher(t);
vector<int> leftLensS(sLen + 1, 0);
vector<int> leftLensT(tLen + 1, 0);
for (int i = 0; i <= 2 * sLen; ++i) chmax(leftLensS[(i + rsS[i]) / 2], rsS[i]);
for (int i = sLen; --i >= 0; ) chmax(leftLensS[i], leftLensS[i + 1] - 2);
for (int i = 0; i <= 2 * tLen; ++i) chmax(leftLensT[(i + rsT[i]) / 2], rsT[i]);
for (int i = tLen; --i >= 0; ) chmax(leftLensT[i], leftLensT[i + 1] - 2);
// cerr<<s<<" "<<rsS<<" "<<leftLensS<<endl;
// cerr<<t<<" "<<rsT<<" "<<leftLensT<<endl;
vector<int> fail(sLen + 1, 0);
{
int j = 0, c = 0;
for (int i = 2; i <= sLen; ++i) {
for (; ; j = fail[j]) {
chmax(c, 2 * i - j);
for (; c + rsS[c - 1] < 2 * i; ++c) {}
if (leftLensS[j + 1] == 2 * i - c + 1) break;
}
fail[i] = ++j;
}
}
// cerr<<"fail = "<<fail<<endl;
int ma = 0;
{
int j = 0, c = 0;
for (int i = 1; i <= tLen; ++i) {
for (; ; j = fail[j]) {
chmax(c, 2 * i - j);
for (; c + rsT[c - 1] < 2 * i; ++c) {}
if (leftLensS[j + 1] == 2 * i - c + 1) break;
}
if (++j == sLen) {
// cerr<<"match ["<<i-sLen<<", "<<i<<")"<<endl;
++ma;
j = fail[j];
}
}
}
return ma;
}
vector<int> run() {
cerr<<"[slow::run]"<<endl;
vector<int> ans(Q);
for (int q = 0; q < Q; ++q) {
ans[q] = solve(S[I[q]], S[J[q]]);
}
return ans;
}
} // slow
int main() {
for (; ~scanf("%d%d%d", &T, &N, &Q); ) {
assert(T == 0);
S.resize(N + 1);
for (int i = 1; i <= N; ++i) {
scanf("%s", buf);
S[i] = buf;
}
S[0] = "";
I.resize(Q);
J.resize(Q);
for (int q = 0; q < Q; ++q) {
scanf("%d%d", &I[q], &J[q]);
}
bool spe2 = true;
for (int i = 1; i <= N; ++i) {
spe2 = spe2 && (S[1].size() == S[i].size());
}
vector<int> ans;
if (spe2) {
ans = sub2::run();
} else if (Q == 1) {
ans = slow::run();
} else {
ans = brute::run();
}
for (int q = 0; q < Q; ++q) {
printf("%d\n", ans[q]);
}
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Checker Judgement Failed
Test #1:
score: 5
Accepted
time: 131ms
memory: 9028kb
input:
0 2 500000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1 1 1 487 1 0 1 487 0 1 0 0 1 1 1 0 487 0 0 1 487 1 1 1 1 487 1 0 0 0 487 487 1 1 487 487 1 0 1 0 1 0 0 487 1 0 1 0 1 1 487 1 1 1 1 1 1 0 1 0 0 0 0 0 0 487 0 1 1 0 1 0 0 1 0 487 487 487 1 1 487 487 1 1 1 1 1 487 0 487 0 0 0 487 0 1 487 487 1 1 1 0 0 1 1 487 1 1 0 1 1 1 1 1 1 487 1 1 0 1 1 0 0 1 0 1 ...
result:
ok 500000 tokens
Test #2:
score: 0
Accepted
time: 129ms
memory: 8988kb
input:
0 2 500000 bbbbaaabbabababbbabaaaabaaabbaababbababaababbaabbaabbaaabbabaaabbbabbabaaaaabbbabbbaabbabbbaabaaaaabbbbabaaabbbbbbbabaaaabaaabbbbabbbababaaaababaaababbbbbababbbbbababbbaababbabbabaaaabbbaaabaabbbbaabaabaabbaaabbaabaaabbaaaabbaabbaabbbbaaabbbbabababaaabbbbaaaaababaaaababbbbbababababaaaabba...
output:
1 999 1 999 1 999 1 0 1 1 0 1 0 999 0 1 1 0 999 0 1 1 1 1 999 999 999 1 1 1 1 1 1 1 1 999 0 999 999 999 1 0 1 999 0 999 1 999 1 0 999 1 0 1 1 999 0 1 0 999 999 1 999 999 1 0 1 999 1 1 1 1 1 1 1 999 0 0 1 999 1 1 1 1 1 1 1 999 0 999 1 0 1 1 1 1 999 1 999 1 1 1 0 1 0 999 0 999 0 0 0 0 1 1 999 999 1 0 ...
result:
ok 500000 tokens
Test #3:
score: 0
Accepted
time: 134ms
memory: 8980kb
input:
0 10 500000 baaaaabbaabbabbbbbbaabbbbaaabaaaaabbaaaaabaabaabaaaaaabababbaabababbaabaaaaaaabbbbaabbaaaabbbbaaaaaaaaaaaaabbaabbababaababbababbaaaabaaabaabbaaaabbaaabababbabbbaabbabbaaaabbaaaababbabaabbaabbbabbbbababaabbbaaabaaaabbbaabbabbaaababbbaababaabbbbbbaabaabbbabbabbbabaaabaaabbabbaabbabaaabaaaa...
output:
1 0 0 0 0 225 0 0 0 0 0 0 0 0 0 0 1 35 0 0 1 1 30 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 16 0 0 3 0 0 1 0 0 0 1 0 1 0 0 0 0 54 50 0 1 0 0 0 1 5 0 0 0 63 0 50 175 109 0 0 0 0 0 0 0 0 0 0 1 175 3 109 0 0 0 35 0 0 0 0 0 0 0 0 0 0 50 0 4 0 0 0 0 0 3 0 18 0 0 0 35 301 0 0 0 0 0 0 35 0 0 175 0 0 1 1 0 0 0 0 1 0 0...
result:
ok 500000 tokens
Test #4:
score: 0
Accepted
time: 54ms
memory: 9528kb
input:
0 30 500000 ababbababaabaababaaabbaaabbabaabababaaaababbaaababba abaababaaababaabaabbb baba aaabbaaabbaaabbaa ab bbbbbaabbababbaabaaba bbbbbbabbbbaaaaabaabbaabbabbbaaaaaaaaaabaabb bbb bba baaaaaaabbbababbbaaaabbbba aaabaaaaaaabbbabaaabaaaabbbbabbaabbbaabbbaabaaaaabbabbbabbbabbaaabbbbbbbbabaabbbaabbb...
output:
26 0 0 0 0 0 33 0 0 16 0 1 0 14 0 0 1 0 0 6 4 26 1 0 6 0 14 0 0 50 0 1 0 1 11 0 0 0 0 0 3 0 0 1 0 0 0 0 0 0 0 10 0 3 0 0 2 1 0 0 16 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 3 0 6 0 0 0 165 2 0 0 0 0 0 0 0 0 0 13 0 0 0 36 0 2 10 0 0 1 0 0 0 0 0 36 0 0 14 0 0 0 0 0 2 2 0 0 0 6 1 2 0 1 0 0 13 0 0 1 18 0 0 0 0 0 0...
result:
ok 500000 tokens
Test #5:
score: 0
Accepted
time: 63ms
memory: 9284kb
input:
0 100 500000 a aba aaa aababbbaaaabbabba abaaabb babbabbabbaa bbbb baaabb aaaababbbabbaababaa babaabbab ababbabab aaaba baaabbaabaabb abbababbaababbabaabbbaaaaabababaabab ba bb aaabbbbbbb a bbabaaabababbaa aab bba aaa abb b bbbbab abab bbaab bbbabbbabbbabaabbabb ababa bb babaaaaaaaaaaaabaabaaaaaa ba...
output:
1 0 0 0 0 0 0 37 13 0 0 0 0 2 0 1 0 0 5 0 0 0 0 0 2 10 0 0 7 0 0 3 0 0 3 0 3 0 0 3 13 0 0 2 0 0 0 0 8 5 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 27 0 5 3 0 0 1 0 0 0 6 0 0 3 0 0 0 0 0 0 0 2 2 0 0 5 0 9 0 0 0 6 0 0 0 0 0 0 0 0 6 0 0 0 1 5 21 0 8 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 1 0 0 1 2 0 0 3 0 0 0 0 1 0 25 8 1...
result:
ok 500000 tokens
Test #6:
score: -5
Checker Judgement Failed
input:
0 10 500000 dwufssiqlihcwjeunletiposagglzrtaebzfwarrgxpffcgdfjlkyaiaprxqtlnmdweczqpbcqppbaeghojhodqyicl dskgaujjtcaaiobkallyeoyddhqgfsrpyghyqnpxihmlyg qwpqdmsygfqpyotyqayhprdccctiunclkqeguxmlvplrxauwxwdccmfepmrivspacjlkncuylibvguvqvncoykwtjuxorgiiqpmotlhmkmqdaxtiiqamskzledzucczsygneqrmzrikhffuqbclgf...
output:
result:
Subtask #2:
score: 0
Judgement Failed
Test #10:
score: 0
Judgement Failed
input:
0 500000 500000 v s o w f c z u d b z h b e w p n l e i e h g h o q u x n k t z i f e t q b s h o q k n k t d x t u p w l h g j c q n i s o v s u e n c j f u w q g u p v w z w p r d n m v d z n j l o n v y u j x j v a e x r l s x g u a h u c b z k b t g h o g k t l u i c q p v c s s s l i c h t o s ...
output:
result:
Subtask #3:
score: 0
Judgement Failed
Test #32:
score: 0
Judgement Failed
input:
0 1 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%