QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#553076 | #7705. Make Your Own Morse Code Palindrome | stig# | AC ✓ | 1ms | 3808kb | C++20 | 3.9kb | 2024-09-08 07:11:22 | 2024-09-08 07:11:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
typedef int uci;
#define int long long
#define ld long double
#define sz(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
const int BIG = 1e5;
vector<string> table = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--..",
"-----",
".----",
"..---",
"...--",
"....-",
".....",
"-....",
"--...",
"---..",
"----."
};
uci main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector<char> dict;
map<char, int> undict;
map<string, char> unmorse;
for (char x = 'A'; x <= 'Z'; x++) dict.push_back(x);
for (char x = '0'; x <= '9'; x++) dict.push_back(x);
for (int i = 0; i < sz(dict); i++) {
undict[dict[i]] = i;
unmorse[table[i]] = dict[i];
}
string s;
getline(cin, s);
string processed = "";
for (char c : s) {
if (undict.contains(c)) {
processed += c;
}
}
s = processed;
string ms = "";
for (char c : s) {
ms += table[undict[c]];
}
// first check if already palindrome
bool good = 1;
for (int i = 0; i < sz(ms) / 2; i++) {
if (ms[i] != ms[sz(ms) - i - 1]) {
good = 0;
break;
}
}
if (good) {
cout << 0 << '\n';
return 0;
}
int ans = BIG;
string best = "";
// not already a palindrome so lets reflect range [0, p] and run dp
// 0111 | --------
// ^-p
// reflect = 1110
for (int p = 0; p < sz(ms); p++) {
// first the section following p must be a palindrome
bool good = 1;
for (int i = 0; i < (sz(ms) - p - 1) / 2; i++) {
if (ms[p + 1 + i] != ms[sz(ms) - i - 1]) {
good = 0;
break;
}
}
if (!good) continue;
// now we can run dp to find min to compose reflect([0, p])
string reflect = ms.substr(0, p + 1);
reverse(all(reflect));
int len = sz(reflect);
vector<int> dp(len + 1, BIG);
vector<string> dps(len + 1);
dp[0] = 0;
dps[0] = "";
for (int i = 1; i <= len; i++) {
for (int j = 1; j <= 5; j++) {
if (i - j < 0) break;
string sect = reflect.substr(i - j, j);
if (unmorse.contains(sect)) {
if (dp[i - j] + 1 < dp[i]) {
dp[i] = dp[i - j] + 1;
dps[i] = dps[i - j] + unmorse[sect];
}
}
}
}
if (dp[len] < ans) {
ans = dp[len];
best = dps[len];
}
}
cout << ans << ' ' << best << '\n';
}
// FOOTS -> 3 0QI -------.-..
// FOOTS -> 3 0GD -------.-..
// 0 -> -----
// Q -> --.-
// I -> ..
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
FOOT
output:
1 L
result:
ok correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
FOOTS
output:
3 0QI
result:
ok correct
Test #3:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
FOOTS
output:
3 0QI
result:
ok correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
FOOTSTOOL
output:
0
result:
ok correct
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
OOTNX
output:
2 J0
result:
ok correct
Test #6:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
3 FRENCH HENS
output:
6 5RCLXB
result:
ok correct
Test #7:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
TWAS THE NIGHT BEFORE XMAS
output:
13 YXFOL46PF4VPT
result:
ok correct
Test #8:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
1A2B3C4D5E6F7G8H9I10J
output:
18 0695OW3L4535Y62X1E
result:
ok correct
Test #9:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
A PARTRIDGE IN A PEAR TREE
output:
8 QF3FFCXC
result:
ok correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
TEN LORDS ALEAPING
output:
9 BQ4L4R8XA
result:
ok correct
Test #11:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
XABCDQRST1234567890123456789
output:
7 6CZCBQU
result:
ok correct
Test #12:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
ASDFGHJKLQWERTYUIOPMNBVCXZ987
output:
23 ZO12FY5ROP8XYLQPRY73LFF
result:
ok correct
Test #13:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
THE QUICK BROWN FOX JUMPS OVER
output:
17 24YXQ2C2JR3RCLY5T
result:
ok correct
Test #14:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
1234567891 IS A PRIME NUMBER 0
output:
18 L37YVUC59123456789
result:
ok correct
Test #15:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
INTRANSIGENCE
output:
0
result:
ok correct
Test #16:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
ANTICKING ANTICKING WRECKING A
output:
5 CRCXP
result:
ok correct
Test #17:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
output:
1 E
result:
ok correct
Test #18:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
ISO9001 "IEEEEEEEEEEEEEEEEEEEE
output:
6 90018S
result:
ok correct