0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 17:32:00 +00:00

#1385. 回文(pali)

https://sjzezoj.com/submission/72189
This commit is contained in:
Baoshuo Ren 2023-02-24 06:59:52 +08:00
parent cb74a3aa99
commit 3d921fdb05
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
42 changed files with 226 additions and 0 deletions

103
S2OJ/1385/1385.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
void solve() {
std::vector<int> st(3), ed(3);
std::string s;
for (int i = 0; i < 3; i++) {
std::string t;
cin >> t;
st[i] = s.size();
s += t;
ed[i] = s.size() - 1;
}
std::function<int(int)> id = [&](int x) -> int {
return std::distance(std::begin(ed), std::lower_bound(std::begin(ed), std::end(ed), x));
};
int n = s.size();
std::vector<std::vector<long long>>
f(n, std::vector<long long>(n)),
g(3, std::vector<long long>(n)),
h(3, std::vector<long long>(n));
long long w = 0, ans = 0;
for (int i = 0; i < n; i++) {
for (int j = n - 1; j > i; j--) {
if (s[i] != s[j]) continue;
int id_i = id(i),
id_j = id(j);
if (id_i == 0 && id_j == 2) {
f[i][j] = 1;
}
f[i][j] += g[id_j][i]; // 加上右边块的贡献
f[i][j] += h[id_i][j]; // 加上左边块的贡献
if (id_i == 1 && id_j == 1) {
f[i][j] += w; // 加上两边来的贡献
}
if (i != st[id_i] && j != ed[id_j]) {
f[i][j] += f[i - 1][j + 1]; // 还在块内,不跳到下一块
}
if (id_i == 0 && id_j == 2) {
w += f[i][j]; // 如果在两边的块,更新贡献
}
if (id_j != 0 && i != ed[id_i]) {
g[id_j - 1][i + 1] += f[i][j]; // 更新到左边块的贡献
}
if (id_i != 2 && j != st[id_j]) {
h[id_i + 1][j - 1] += f[i][j]; // 更新到右边块的贡献
}
if (id_i == id_j) {
if (j - i + 1 <= 3) {
// j - i + 1 == 2: i, j 相邻,总长度为偶数
// == 3: i, j 之间还有一个字符,总长度为奇数
ans += f[i][j];
}
} else if (id_i + 1 == id_j) { // i 和 j 在相邻块中
ans += static_cast<long long>(
1 // i, j 之间不拼字符,总长度为偶数
+ (i != ed[id_i]) // 如果 i 不是块末尾,还可以在中间拼一个字符,总长度为奇数
+ (j != st[id_j]) // 如果 j 不是块开头,还可以在中间拼一个字符,总长度为奇数
)
* f[i][j];
}
}
}
ans += w * (ed[1] - st[1] + 1);
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) solve();
return 0;
}

BIN
S2OJ/1385/data/pali1.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali10.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali11.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali12.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali13.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali14.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali15.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali16.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali17.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali18.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali19.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali2.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali20.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali3.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali4.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali5.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali6.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali7.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali8.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali9.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/pali9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1385/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.