QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#723927 | #7705. Make Your Own Morse Code Palindrome | MattTheNub# | AC ✓ | 117ms | 3856kb | C++23 | 6.3kb | 2024-11-08 03:47:10 | 2024-11-08 03:47:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using v = vector<T>;
using ll = long long;
using dd = long double;
using int2 = pair<int, int>;
using ll2 = pair<ll, ll>;
using dd2 = pair<dd, dd>;
#define f first
#define s second
#define all(x) begin(x), end(x)
istream &__cin = cin;
#ifdef DEV_MODE
#include "debug.h"
__cinwrapper __cin_wrapper;
#define cin __cin_wrapper
#else
#define dbg(...)
#define dbg2d(...)
#endif
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T> istream &operator>>(istream &in, v<T> &v) {
for (auto &x : v)
in >> x;
return in;
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
_______________________________________
( If you don't fail at least 90% of the )
( time, you're not aiming high enough. )
( )
( - Alan Kay )
---------------------------------------
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||
*/
const bool INTERACTIVE = false;
const bool MULTITEST = false;
/******************************************************************************/
#pragma region templates
#pragma endregion templates
const string code[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", "-----", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----."};
string get(char c) {
if (c >= 'A' && c <= 'Z') {
return code[c - 'A'];
} else if (c >= '0' && c <= '9') {
return code[c - '0' + 26];
} else {
return "";
}
}
char unget(int i) {
if (i < 26)
return i + 'A';
else
return i + '0' - 26;
}
void solve() {
string s;
getline(__cin, s);
string t;
for (auto x : s) {
t.append(get(x));
}
string r = t;
// reverse(all(r));
// if (t == r) {
// cout << 0;
// return;
// }
// reverse(all(r));
pair<int, string> ans = {1e9, "pls no"};
for (int i = 0; i <= (int)t.size(); i++) {
bool ok = 1;
for (int j = 0; j < (int)t.size(); j++) {
int k = (int)t.size() + i - j - 1;
if (k < (int)t.size() && t[j] != t[k])
ok = 0;
}
if (!ok)
continue;
v<int2> dp(i + 1, {1e9, -1});
dp[0] = {0, -1};
for (int j = 1; j <= i; j++) {
for (int k = 0; k < 36; k++) {
if ((int)code[k].size() <= j) {
bool ok = 1;
for (int p = 0; p < (int)code[k].size(); p++) {
if (code[k][p] != t[i - j - p + (int)code[k].size() - 1])
ok = 0;
}
if (ok && dp[j - (int)code[k].size()].f < dp[j].f) {
dp[j] = {dp[j - (int)code[k].size()].f + 1, k};
}
}
}
}
string res;
if (dp[i].s != -1) {
int cur = i;
while (cur != 0) {
res.push_back(unget(dp[cur].s));
cur -= code[dp[cur].s].size();
}
reverse(all(res));
}
dbg(res);
ans = min(ans, {dp[i].f, res});
}
for (int I = 0; I < 36; I++) {
t = r + code[I];
for (int i = 0; i <= (int)t.size(); i++) {
bool ok = 1;
for (int j = 0; j < (int)t.size(); j++) {
int k = (int)t.size() + i - j - 1;
if (k < (int)t.size() && t[j] != t[k])
ok = 0;
}
if (!ok)
continue;
v<int2> dp(i + 1, {1e9, -1});
dp[0] = {0, -1};
for (int j = 1; j <= i; j++) {
for (int k = 0; k < 36; k++) {
if ((int)code[k].size() <= j) {
bool ok = 1;
for (int p = 0; p < (int)code[k].size(); p++) {
if (code[k][p] != t[i - j - p + (int)code[k].size() - 1])
ok = 0;
}
if (ok && dp[j - (int)code[k].size()].f < dp[j].f) {
dp[j] = {dp[j - (int)code[k].size()].f + 1, k};
}
}
}
}
string res;
if (dp[i].s != -1) {
int cur = i;
while (cur != 0) {
res.push_back(unget(dp[cur].s));
cur -= code[dp[cur].s].size();
}
reverse(all(res));
}
dbg(res);
ans = min(ans, {dp[i].f + 1, unget(I) + res});
}
}
for (int I = 0; I < 36; I++) {
for (int J = 0; J < 36; J++) {
t = r + code[I] + code[J];
for (int i = 0; i <= (int)t.size(); i++) {
bool ok = 1;
for (int j = 0; j < (int)t.size(); j++) {
int k = (int)t.size() + i - j - 1;
if (k < (int)t.size() && t[j] != t[k])
ok = 0;
}
if (!ok)
continue;
v<int2> dp(i + 1, {1e9, -1});
dp[0] = {0, -1};
for (int j = 1; j <= i; j++) {
for (int k = 0; k < 36; k++) {
if ((int)code[k].size() <= j) {
bool ok = 1;
for (int p = 0; p < (int)code[k].size(); p++) {
if (code[k][p] != t[i - j - p + (int)code[k].size() - 1])
ok = 0;
}
if (ok && dp[j - (int)code[k].size()].f < dp[j].f) {
dp[j] = {dp[j - (int)code[k].size()].f + 1, k};
}
}
}
}
string res;
if (dp[i].s != -1) {
int cur = i;
while (cur != 0) {
res.push_back(unget(dp[cur].s));
cur -= code[dp[cur].s].size();
}
reverse(all(res));
}
dbg(res);
ans = min(ans, {dp[i].f + 2, unget(I) + (unget(J) + res)});
}
}
}
cout << ans.f << ' ' << ans.s << '\n';
}
int main() {
#ifdef DEV_MODE
debug_start(INTERACTIVE, "l.txt");
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
if (MULTITEST)
cin >> t;
else
t = 1;
while (t--)
solve();
#ifdef DEV_MODE
debug_exit(INTERACTIVE);
#endif
}
#ifdef DEV_MODE
#include "debug.cpp"
#endif
详细
Test #1:
score: 100
Accepted
time: 7ms
memory: 3764kb
input:
FOOT
output:
1 L
result:
ok correct
Test #2:
score: 0
Accepted
time: 10ms
memory: 3832kb
input:
FOOTS
output:
3 0GD
result:
ok correct
Test #3:
score: 0
Accepted
time: 13ms
memory: 3624kb
input:
FOOTS
output:
3 0GD
result:
ok correct
Test #4:
score: 0
Accepted
time: 23ms
memory: 3596kb
input:
FOOTSTOOL
output:
0
result:
ok correct
Test #5:
score: 0
Accepted
time: 13ms
memory: 3616kb
input:
OOTNX
output:
2 J0
result:
ok correct
Test #6:
score: 0
Accepted
time: 30ms
memory: 3616kb
input:
3 FRENCH HENS
output:
6 5RCRL7
result:
ok correct
Test #7:
score: 0
Accepted
time: 45ms
memory: 3840kb
input:
TWAS THE NIGHT BEFORE XMAS
output:
13 F8XJL46PF4VPT
result:
ok correct
Test #8:
score: 0
Accepted
time: 76ms
memory: 3616kb
input:
1A2B3C4D5E6F7G8H9I10J
output:
18 06M73Y3L4535YN59R9
result:
ok correct
Test #9:
score: 0
Accepted
time: 38ms
memory: 3804kb
input:
A PARTRIDGE IN A PEAR TREE
output:
8 QF3FFCXC
result:
ok correct
Test #10:
score: 0
Accepted
time: 33ms
memory: 3776kb
input:
TEN LORDS ALEAPING
output:
9 BG6C5C8DK
result:
ok correct
Test #11:
score: 0
Accepted
time: 117ms
memory: 3696kb
input:
XABCDQRST1234567890123456789
output:
7 6CZKLPX
result:
ok correct
Test #12:
score: 0
Accepted
time: 83ms
memory: 3856kb
input:
ASDFGHJKLQWERTYUIOPMNBVCXZ987
output:
23 3212FY5ROP8XYLQPRY73LFF
result:
ok correct
Test #13:
score: 0
Accepted
time: 63ms
memory: 3568kb
input:
THE QUICK BROWN FOX JUMPS OVER
output:
17 24YXQ2C2JR3RTLXP4
result:
ok correct
Test #14:
score: 0
Accepted
time: 76ms
memory: 3684kb
input:
1234567891 IS A PRIME NUMBER 0
output:
18 A59F7XC59123456789
result:
ok correct
Test #15:
score: 0
Accepted
time: 23ms
memory: 3632kb
input:
INTRANSIGENCE
output:
0
result:
ok correct
Test #16:
score: 0
Accepted
time: 53ms
memory: 3640kb
input:
ANTICKING ANTICKING WRECKING A
output:
5 CRCXP
result:
ok correct
Test #17:
score: 0
Accepted
time: 52ms
memory: 3552kb
input:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
output:
1 E
result:
ok correct
Test #18:
score: 0
Accepted
time: 44ms
memory: 3568kb
input:
ISO9001 "IEEEEEEEEEEEEEEEEEEEE
output:
6 110Y05
result:
ok correct