QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#331977 | #7705. Make Your Own Morse Code Palindrome | cry# | RE | 3ms | 3576kb | C++14 | 5.9kb | 2024-02-19 03:16:20 | 2024-02-19 03:16:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1E9;
map<char, string> mp;
map<char, string> revmp;
vector<char> chars;
void envy(){
mp['A'] = "01";
mp['B'] = "1000";
mp['C'] = "1010";
mp['D'] = "100";
mp['E'] = "0";
mp['F'] = "0010";
mp['G'] = "110";
mp['H'] = "0000";
mp['I'] = "OO";
mp['J'] = "0111";
mp['K'] = "101";
mp['L'] = "0100";
mp['M'] = "11";
mp['N'] = "10";
mp['O'] = "111";
mp['P'] = "0110";
mp['Q'] = "1101";
mp['R'] = "010";
mp['S'] = "000";
mp['T'] = "1";
mp['U'] = "001";
mp['V'] = "0001";
mp['W'] = "011";
mp['X'] = "1001";
mp['Y'] = "1011";
mp['Z'] = "1100";
mp['0'] = "11111";
mp['1'] = "01111";
mp['2'] = "00111";
mp['3'] = "00011";
mp['4'] = "00001";
mp['5'] = "00000";
mp['6'] = "10000";
mp['7'] = "11000";
mp['8'] = "11100";
mp['9'] = "11110";
for(char c = 'A'; c <= 'Z'; c++){
revmp[c] = mp[c];
reverse(revmp[c].begin(), revmp[c].end());
chars.push_back(c);
}
for(char c = '0'; c <= '9'; c++){
revmp[c] = mp[c];
reverse(revmp[c].begin(), revmp[c].end());
chars.push_back(c);
}
}
struct item{
string morse;
vector<char> v;
};
// build using mp
string to_build(string s){
vector<int> dp(s.size() + 1, INF);
vector<pair<int, char>> prv(s.size() + 1, {-1, '$'});
dp[0] = 0;
for(int i = 0; i < s.size(); i++) {
if(dp[i] == INF) {
continue;
}
for(pair<char, string> j : mp) {
if(i + j.second.size() <= s.size() && s.substr(i, j.second.size()) == j.second && dp[i + j.second.size()] > dp[i] + 1) {
dp[i + j.second.size()] = dp[i] + 1;
prv[i + j.second.size()] = {i, j.first};
}
}
}
string ans = "";
for(int i = s.size(); i; i = prv[i].first) {
ans.push_back(prv[i].second);
}
reverse(ans.begin(), ans.end());
return ans;
}
string g(string morsed) {
string ans = "$";
for(int i = 0; i <= morsed.size(); i++) {
string shudbp = morsed.substr(i);
string rp = shudbp; reverse(rp.begin(), rp.end());
if(rp == shudbp) {
string make = morsed.substr(0, i);
reverse(make.begin(), make.end());
if(ans == "$") {
ans = to_build(make);
} else {
string s2 = to_build(make);
if(s2.size() < ans.size()) {
ans = s2;
}
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
envy();
string s;
getline(cin, s);
string ns;
for(int i = 0; i < s.size(); i++){
if(s[i] >= 'A' && s[i] <= 'Z'){
ns.push_back(s[i]);
}
if(s[i] >= '0' && s[i] <= '9'){
ns.push_back(s[i]);
}
}
s = ns;
string morsed;
for(char c: s){
morsed += mp[c];
}
string ans = g(morsed);
for(pair<char, string> i : mp) {
string s2 = string(1, i.first) + g(morsed + i.second);
if(s2.size() < ans.size()) {
ans = s2;
}
}
string rv = morsed;
for(char c : ans) {
rv += mp[c];
}
string rv2 = rv;
reverse(rv2.begin(), rv2.end());
assert(rv == rv2);
cout << ans.size() << " " << ans << "\n";
/* string reverse_morsed = morsed;
reverse(reverse_morsed.begin(), reverse_morsed.end());
int n = morsed.size();
// cout << morsed << endl;
vector<char> ans;
for(int i = 0; i < 100; i++){
ans.push_back('A');
}
queue<item> q;
q.push({"", {}});
while(!q.empty()){
auto [str, v] = q.front();
q.pop();
// cout << str << endl;
if(v.size() > ans.size()) continue;
{
string reverse_str = str;
reverse(reverse_str.begin(), reverse_str.end());
string ns = morsed + reverse_str;
string nns = ns;
reverse(ns.begin(), ns.end());
if(nns == ns){
if(v.size() < ans.size()){
ans = v;
continue;
}
}
}
// if(str.size() >= n){
// string overflow = str.substr(n);
// reverse(overflow.begin(), overflow.end());
// vector<char> to_construct = to_build(overflow);
// if(!(to_construct.size() == 1 && to_construct[0] == '!')){
// vector<char> final_v;
// for(char c: to_construct){
// final_v.push_back(c);
// }
// reverse(v.begin(), v.end());
// for(char c: v){
// final_v.push_back(c);
// }
// if(final_v.size() < ans.size()){
// ans = final_v;
// }
// continue;
// }
// }
for(char c: chars){
string ns = str + revmp[c];
int n = min(ns.length(), reverse_morsed.length());
if(ns.substr(0, n) == reverse_morsed.substr(0, n)){
vector<char> nv = v;
nv.push_back(c);
q.push({ns, nv});
}
}
}
cout << ans.size() << " ";
for(char c: ans) cout << c; */
// int n; int m;
// cin >> n >> m;
// vector<array<int, 2>> p(n);
// for(int i = 0; i < n; i++) {
// cin >> p[i][0] >> p[i][1];
// }
// vector<vector<array<int, 3>>> adj(n, vector<array<int, 2>>(4, {-1, -1}));
// vector<int> inter(m);
// for(int i = 0; i < m; i++) {
// int ii; int ji; int ki;
// cin >> ii >> ji >> ki;
// ii--; ji--;
// inter[i] = ki;
// if(p[ii][0] < p[ji][0]) {
// adj[ii][1] = {ji, i};
// } else if(p[ii][0] > p[ji][0]) {
// } else if(p[ii][1] < p[ji][1]) {
// } else {
// }
// }
}
// string s; cin >> s;
// int n = s.size();
// for(int i = 0; i < n - 1; i++){
// if(s[i] == 'O' && s[i + 1] == 'O'){
// cout << "INVALID" << endl;
// return 0;
// }
// }
// if(s.back() != 'O'){
// cout << "INVALID" << endl;
// return 0;
// }
// for(ll start = 3; start < 1000; start += 2){
// ll res = start;
// bool ok = true;
// for(int i = n - 2; i >= 0; i--){
// if(s[i] == 'E'){
// res *= 2;
// }
// else{
// res--;
// if(res % 3 != 0){
// ok = false;
// break;
// }
// res /= 3;
// }
// if(s[i] == 'E' && res % 2 == 1){
// ok = false;
// break;
// }
// if(s[i] == 'O' && res % 2 == 0){
// ok = false;
// break;
// }
// if(__builtin_popcountll(res) == 1){
// ok = false;
// break;
// }
// }
// if(ok){
// cout << res << "\n";
// // break;
// }
// }
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3552kb
input:
FOOT
output:
1 L
result:
ok correct
Test #2:
score: 0
Accepted
time: 2ms
memory: 3560kb
input:
FOOTS
output:
3 M0L
result:
ok correct
Test #3:
score: 0
Accepted
time: 2ms
memory: 3560kb
input:
FOOTS
output:
3 M0L
result:
ok correct
Test #4:
score: 0
Accepted
time: 3ms
memory: 3576kb
input:
FOOTSTOOL
output:
0
result:
ok correct
Test #5:
score: 0
Accepted
time: 2ms
memory: 3556kb
input:
OOTNX
output:
2 J0
result:
ok correct
Test #6:
score: -100
Runtime Error
input:
3 FRENCH HENS