QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#604078 | #7705. Make Your Own Morse Code Palindrome | zkxaqygpzwlvbrxzso | AC ✓ | 1ms | 3824kb | C++14 | 5.1kb | 2024-10-01 22:56:29 | 2024-10-01 22:56:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main(){
// Define the Morse code mapping
// Letters A-Z
vector<pair<char, string>> morse_map = {
{'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."},
{'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."},
{'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."},
{'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."},
{'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"},
{'Y', "-.--"}, {'Z', "--.."},
// Digits 0-9
{'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"},
{'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."},
{'8', "---.."}, {'9', "----."}
};
// Create a map from Morse code to character for quick lookup
unordered_map<string, char> morse_to_char;
for(auto &p : morse_map){
morse_to_char[p.second] = p.first;
}
// Read the input
string input;
getline(cin, input);
// Filter the input to keep only alphanumeric characters and convert to uppercase
string filtered;
for(char c : input){
if(isalnum(c)){
filtered += toupper(c);
}
}
// Convert to Morse code
string S = "";
for(char c : filtered){
// Find the Morse code for c
// 'A' to 'Z' are first 26, '0' to '9' are next 10
string mc = "";
if(c >= 'A' && c <= 'Z'){
mc = morse_map[c - 'A'].second;
}
else { // '0' to '9'
mc = morse_map[26 + (c - '0')].second;
}
S += mc;
}
// Check if S is a palindrome
string RevS = S;
reverse(RevS.begin(), RevS.end());
if(S == RevS){
cout << "0" << endl;
return 0;
}
// Initialize variables to store the minimal number of characters and the append string
int min_chars = INT32_MAX;
string min_append = "";
// Precompute the reverse of S
string reversed_S = RevS;
// Precompute the length of S
int N = S.length();
// Iterate k from 0 to len(RevS)
for(int k=0; k <= reversed_S.length(); k++){
// Check if S ends with reversed_S[0:k]
if(k > N) continue; // Cannot have overlap longer than S
bool match = true;
for(int i=0; i<k; i++){
if(S[N - k + i] != reversed_S[i]){
match = false;
break;
}
}
if(!match) continue;
// T is reversed_S[k:]
string T = reversed_S.substr(k);
// Now, perform word break on T
int L = T.length();
// Initialize DP
vector<int> dp(L + 1, INT32_MAX);
dp[0] = 0;
// To reconstruct the path
vector<char> back_char(L + 1, 0);
// Iterate over T
for(int i=1; i<=L; i++){
for(auto &p : morse_map){
string mc = p.second;
int len_c = mc.length();
if(i >= len_c && T.substr(i - len_c, len_c) == mc){
if(dp[i - len_c] != INT32_MAX && dp[i - len_c] + 1 < dp[i]){
dp[i] = dp[i - len_c] + 1;
back_char[i] = p.first;
}
}
}
}
// Check if T can be built
if(dp[L] != INT32_MAX){
// Reconstruct the string
string append_str = "";
int idx = L;
while(idx > 0){
char c = back_char[idx];
append_str += c;
// Find the length of Morse code for c
string mc = "";
if(c >= 'A' && c <= 'Z'){
mc = morse_map[c - 'A'].second;
}
else { // '0' to '9'
mc = morse_map[26 + (c - '0')].second;
}
idx -= mc.length();
}
// Since we reconstructed in reverse, reverse it back
reverse(append_str.begin(), append_str.end());
// Update minimal characters and append string
if(dp[L] < min_chars){
min_chars = dp[L];
min_append = append_str;
}
// If same number of characters, we can keep the first one found
}
}
// After all k, check if we found a solution
if(min_chars != INT32_MAX){
// If min_chars is 0, it means no need to append
if(min_chars == 0){
cout << "0" << endl;
}
else{
cout << min_chars;
// According to the problem statement, if not 0, we need to output a space and the append string
cout << " " << min_append << endl;
}
}
else{
// According to the problem statement, it's always possible, but to be safe
// Output the entire reversed_S as appended string
// But need to build it from Morse codes, which should have been handled above
// So this case should not occur
cout << "0" << endl;
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3824kb
input:
FOOT
output:
1 L
result:
ok correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
FOOTS
output:
3 30L
result:
ok correct
Test #3:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
FOOTS
output:
3 30L
result:
ok correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
FOOTSTOOL
output:
0
result:
ok correct
Test #5:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
OOTNX
output:
2 J0
result:
ok correct
Test #6:
score: 0
Accepted
time: 1ms
memory: 3696kb
input:
3 FRENCH HENS
output:
6 HFCLXB
result:
ok correct
Test #7:
score: 0
Accepted
time: 1ms
memory: 3476kb
input:
TWAS THE NIGHT BEFORE XMAS
output:
13 F8XJL46PF4VWA
result:
ok correct
Test #8:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
1A2B3C4D5E6F7G8H9I10J
output:
18 10484QBQ5L5P4J51F9
result:
ok correct
Test #9:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
A PARTRIDGE IN A PEAR TREE
output:
8 QF3FFCXC
result:
ok correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
TEN LORDS ALEAPING
output:
9 BQ4LHC8XA
result:
ok correct
Test #11:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
XABCDQRST1234567890123456789
output:
7 6CZCBQU
result:
ok correct
Test #12:
score: 0
Accepted
time: 1ms
memory: 3816kb
input:
ASDFGHJKLQWERTYUIOPMNBVCXZ987
output:
23 3212FY5AJP8XYLQWFY73LFF
result:
ok correct
Test #13:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
THE QUICK BROWN FOX JUMPS OVER
output:
17 24YXQ2C2JLUCCLYHA
result:
ok correct
Test #14:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
1234567891 IS A PRIME NUMBER 0
output:
18 Q59F7XC59123456789
result:
ok correct
Test #15:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
INTRANSIGENCE
output:
0
result:
ok correct
Test #16:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
ANTICKING ANTICKING WRECKING A
output:
5 KFCXP
result:
ok correct
Test #17:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
output:
1 R
result:
ok correct
Test #18:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
ISO9001 "IEEEEEEEEEEEEEEEEEEEE
output:
6 110Y05
result:
ok correct