QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#321697#7705. Make Your Own Morse Code Palindromeishmeal#AC ✓7ms3848kbC++232.3kb2024-02-05 07:42:092024-02-05 07:42:10

Judging History

你现在查看的是最新测评结果

  • [2024-02-05 07:42:10]
  • 评测
  • 测评结果:AC
  • 用时:7ms
  • 内存:3848kb
  • [2024-02-05 07:42:09]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

map<char,string> m{
	{'A', "01"},
	{'B', "1000"},
	{'C', "1010"},
	{'D', "100"},
	{'E', "0"},
	{'F', "0010"},
	{'G', "110"},
	{'H', "0000"},
	{'I', "00"},
	{'J', "0111"},
	{'K', "101"},
	{'L', "0100"},
	{'M', "11"},
	{'N', "10"},
	{'O', "111"},
	{'P', "0110"},
	{'Q', "1101"},
	{'R', "010"},
	{'S', "000"},
	{'T', "1"},
	{'U', "001"},
	{'V', "0001"},
	{'W', "011"},
	{'X', "1001"},
	{'Y', "1011"},
	{'Z', "1100"},
	{'0', "11111"},
	{'1', "01111"},
	{'2', "00111"},
	{'3', "00011"},
	{'4', "00001"},
	{'5', "00000"},
	{'6', "10000"},
	{'7', "11000"},
	{'8', "11100"},
	{'9', "11110"}
};
map<string,char> rm;

int best = INT_MAX;
string ans = "";
void build(string s, char st = ' ') {
	reverse(s.begin(),s.end());
	int n = s.size();
	vector<pair<int,string>> dp(n+1, {INT_MAX,""});
	dp[0] = {st != ' ', ""+st};

	for (int i = 0; i < n; i++) {
		for (int j = 1; j <= 5; j++) {
			if (i+j > n) break;
			string t = s.substr(i,j);
			if (rm.count(t)) {
				dp[i+j] = min(dp[i+j], {dp[i].first+1, dp[i].second+rm[t]});
			}
		}
	}
	if (dp[n].first < best) {
		best = dp[n].first;
		ans = dp[n].second;
	}
}

string s;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	for (auto [c, ss] : m) rm[ss] = c;
	
	{
		string t; getline(cin, t);
		for (char c : t) if (m.count(c)) s += m[c];
	}
	int n = s.size();

	for (int i = (n-1)/2; i < n; i++) {
		{
		int j = i+1;
		int i = j-1;
		bool good = true;
		while (j < n) {
			if (s[i] != s[j]) good = false;
			j++,i--;
		}
		if (good) build(s.substr(0, i+1));
		}
	}
	for (int i = n/2; i < n; i++) {
		{
		int j = i;
		int i = j;
		bool good = true;
		while (j < n) {
			if (s[i] != s[j]) good = false;
			j++,i--;
		}
		if (good) build(s.substr(0, i+1));
		}
	}

	for (auto [c, ss] : m) {
		string t = s + ss;
		for (int i = s.size(); i < t.size(); i++) {
			{
			int j = i+1;
			int i = j-1;
			bool good = true;
			while (j < t.size()) {
				if (t[i] != t[j]) good = false;
				j++,i--;
			}
			if (good) build(t.substr(0, i+1), c);
			}
		}
		for (int i = s.size(); i < t.size(); i++) {
			{
			int j = i;
			int i = j;
			bool good = true;
			while (j < t.size()) {
				if (t[i] != t[j]) good = false;
				j++,i--;
			}
			if (good) build(t.substr(0, i+1), c);
			}
		}
	}

	if (!best) cout << "0\n";
	else cout << best << ' ' << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

FOOT

output:

1 L

result:

ok correct

Test #2:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

FOOTS

output:

3 1OL

result:

ok correct

Test #3:

score: 0
Accepted
time: 1ms
memory: 3832kb

input:

FOOTS

output:

3 1OL

result:

ok correct

Test #4:

score: 0
Accepted
time: 2ms
memory: 3848kb

input:

FOOTSTOOL

output:

0

result:

ok correct

Test #5:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

OOTNX

output:

2 J0

result:

ok correct

Test #6:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

3 FRENCH HENS

output:

6 5RCLD7

result:

ok correct

Test #7:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

TWAS THE NIGHT BEFORE XMAS

output:

13 R8DKGB3576LRQ

result:

ok correct

Test #8:

score: 0
Accepted
time: 5ms
memory: 3848kb

input:

1A2B3C4D5E6F7G8H9I10J

output:

18 0695MKBQ5L5P4J51F9

result:

ok correct

Test #9:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

A PARTRIDGE IN A PEAR TREE

output:

8 QF3FFCXC

result:

ok correct

Test #10:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

TEN LORDS ALEAPING

output:

9 BG6C5C8DK

result:

ok correct

Test #11:

score: 0
Accepted
time: 7ms
memory: 3580kb

input:

XABCDQRST1234567890123456789

output:

7 6CZCBGX

result:

ok correct

Test #12:

score: 0
Accepted
time: 5ms
memory: 3628kb

input:

ASDFGHJKLQWERTYUIOPMNBVCXZ987

output:

23 AXQ88C7VJP8DQCPYFY73LFF

result:

ok correct

Test #13:

score: 0
Accepted
time: 4ms
memory: 3620kb

input:

THE QUICK BROWN FOX JUMPS OVER

output:

17 24YXQ2C2AG6GCDXP4

result:

ok correct

Test #14:

score: 0
Accepted
time: 5ms
memory: 3636kb

input:

1234567891 IS A PRIME NUMBER 0

output:

18 C48R7XC59123456789

result:

ok correct

Test #15:

score: 0
Accepted
time: 2ms
memory: 3764kb

input:

INTRANSIGENCE

output:

0

result:

ok correct

Test #16:

score: 0
Accepted
time: 3ms
memory: 3772kb

input:

ANTICKING ANTICKING WRECKING A

output:

5 CRCXP

result:

ok correct

Test #17:

score: 0
Accepted
time: 3ms
memory: 3652kb

input:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

output:

1 E

result:

ok correct

Test #18:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

ISO9001 "IEEEEEEEEEEEEEEEEEEEE

output:

6 90018S

result:

ok correct