QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605870#9288. Roman Palindromeslym#WA 1ms3876kbC++204.7kb2024-10-02 20:19:092024-10-02 20:19:10

Judging History

This is the latest submission verdict.

  • [2024-10-02 20:19:10]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3876kb
  • [2024-10-02 20:19:09]
  • Submitted

answer

#include<bits/stdc++.h>
using i64 = long long;

void solve() {
	int n;
	std::cin >> n;
	std::string s;
	std::cin >> s;
	std::vector<std::string > ans;
	std::vector<int> num(n + 1);
 	for (int i = 0; i < n; i ++) {
 		/*
 		if (i + 2 < n) {
 			if (s[i] == 'M' && s[i + 1] == 'C' && s[i + 2] == 'M') {
 				ans.push_back("MCM");
 				i += 2;
 				continue;
 			}
 			if (s[i] == 'C' && s[i + 1] == 'X' && s[i + 2] == 'C') {
 				ans.push_back("CXC");
 				i += 2;
 				continue;
 			}
 			if (s[i] == 'X' && s[i + 1] == 'I' && s[i + 2] == 'X') {
 				ans.push_back("XIX");
 				i += 2;
 				continue;
 			}
 		}*/

 		if (s[i] == 'M') {
 			int j = i;
 			std::string t = "";
 			while (j < n && j - i < 3 && s[j] == 'M') {
 				t += 'M';
 				num[j] = ans.size() + 1;
 				j ++;
 			}
 			i = j - 1;
 			ans.push_back(t);
 		} else if (s[i] == 'C') {
 			int j = i;
 			std::string t = "";
 			while (j < n && j - i < 3 && s[j] == 'C') {
 				t += 'C';
 				num[j] = ans.size() + 1;
 				j ++;
 			}
 			i = j - 1;
 			ans.push_back(t);
 		} else if (s[i] == 'D') { 
 			num[i] = ans.size() + 1;
 			ans.push_back("D");
 		} else if (s[i] == 'X') {
			int j = i;
 			std::string t = "";
 			while (j < n && j - i < 3 && s[j] == 'X') {
 				t += 'X';
 				num[j] = ans.size() + 1;
 				j ++;
 			}
 			i = j - 1;
 			ans.push_back(t);
 		} else if (s[i] == 'L') {
 			num[i] = ans.size() + 1;
 			ans.push_back("L");
 		} else if (s[i] == 'I') {
			int j = i;
 			std::string t = "";
 			while (j < n && j - i < 3 && s[j] == 'I') {
 				t += 'I';
 				num[j] = ans.size() + 1;
 				j ++;
 			}
 			i = j - 1;
 			ans.push_back(t);
 		} else if (s[i] == 'V') {
 			num[i] = ans.size() + 1;
 			ans.push_back("V");
 		} 
	}/*
	std::cout << ans.size() << '\n';
	for (auto it : ans) {
		std::cout << it << '\n';
	}*/

 	// for (int i = 0; i < n; i ++) {
 	// 	std::cout << num[i] << ' ';
 	// }
 	// std::cout << '\n';

 	std::vector<int> pre(n + 1);
 	std::vector<std::string> th(n + 1);
 	for (int i = 0; i < 2; i ++) {
 		pre[i] = i - 1;
 	}
 	for (int i = 2; i < n; i ++) {
 		pre[i] = i - 1;
 		if (num[i] > num[i - 1] + 1) {
 			num[i] = num[i - 1] + 1;
 		}
 		if (s[i - 2] == 'M' && s[i - 1] == 'C' && s[i] == 'M') {
 			int op = i - 3 < 0 ? 0 : num[i - 3];
 			if (op + 1 < num[i]) {
 				num[i] = op + 1;
 				pre[i] = i - 3;
 			}
 			th[i] = "MCM";
 		} 
 		if (s[i - 2] == 'C' && s[i - 1] == 'X' && s[i] == 'C') {
			int op = i - 3 < 0 ? 0 : num[i - 3];
 			if (op + 1 < num[i]) {
 				num[i] = op + 1;
 				pre[i] = i - 3;
 			}
 			th[i] = "CXC";
 		} 
 		if (s[i - 2] == 'X' && s[i - 1] == 'I' && s[i] == 'X') {
 			int op = i - 3 < 0 ? 0 : num[i - 3];
 			if (op + 1 < num[i]) {
 				num[i] = op + 1;
 				pre[i] = i - 3;
 			}
 			th[i] = "XIX";
 		} 
 	}

 	// for (int i = 0; i < n; i ++) {
 	// 	std::cout << num[i] << ' ';
 	// }
 	// std::cout << '\n';


 	// std::cout << num[n - 1] << '\n';

 	int now = n - 1;
 	std::set<int> op;
 	while (now != -1) {
 		if (th[now] != "") {
 			op.insert(now);
 		}
 		now = pre[now];
 	}
 	op.insert(n - 1);
 	int fr = 0;
 	ans.clear();

 	// for (auto it : op) {
 	// 	std::cout << it << ' ';
 	// }
 	// std::cout << '\n';


 	for (auto it : op) {
 		int u = it;
 		if (th[it] != "") u = it - 3;

		for (; fr <= u; fr ++) {
			int i = fr;
			if (s[i] == 'M') {
	 			int j = i;
	 			std::string t = "";
	 			while (j <= u && j - i < 3 && s[j] == 'M') {
	 				t += 'M';
	 				j ++;
	 			}
	 			i = j - 1;
	 			ans.push_back(t);
	 		} else if (s[i] == 'C') {
	 			int j = i;
	 			std::string t = "";
	 			while (j <= u && j - i < 3 && s[j] == 'C') {
	 				t += 'C';
	 				j ++;
	 			}
	 			i = j - 1;
	 			ans.push_back(t);
	 		} else if (s[i] == 'D') { 
	 			ans.push_back("D");
	 		} else if (s[i] == 'X') {
				int j = i;
	 			std::string t = "";
	 			while (j <= u && j - i < 3 && s[j] == 'X') {
	 				t += 'X';
	 				j ++;
	 			}
	 			i = j - 1;
	 			ans.push_back(t);
	 		} else if (s[i] == 'L') {
	 			ans.push_back("L");
	 		} else if (s[i] == 'I') {
				int j = i;
	 			std::string t = "";
	 			while (j <= u && j - i < 3 && s[j] == 'I') {
	 				t += 'I';
	 				j ++;
	 			}
	 			i = j - 1;
	 			ans.push_back(t);
	 		} else if (s[i] == 'V') {
	 			ans.push_back("V");
	 		} 
			fr = i;
		}
		if (th[it] != "") {
			ans.push_back(th[it]);
		}
		fr = it + 1;
 	}
 	std::cout << ans.size() << '\n';
 	for (auto it : ans) {
 		std::cout << it << '\n';
 	}
}
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int t = 1;
	//std::cin >> t;
	while (t --) {
		solve();
	}
	return 0;
}
/*
12
MMMCMMCXCXIX
10
MMCMCMCMCM
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

input:

5
MMXXI

output:

3
MM
XX
I

result:

ok OK!

Test #2:

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

input:

1
I

output:

1
I

result:

ok OK!

Test #3:

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

input:

1
V

output:

1
V

result:

ok OK!

Test #4:

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

input:

1
X

output:

1
X

result:

ok OK!

Test #5:

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

input:

1
L

output:

1
L

result:

ok OK!

Test #6:

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

input:

1
C

output:

1
C

result:

ok OK!

Test #7:

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

input:

1
D

output:

1
D

result:

ok OK!

Test #8:

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

input:

1
M

output:

1
M

result:

ok OK!

Test #9:

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

input:

2
XX

output:

1
XX

result:

ok OK!

Test #10:

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

input:

2
LL

output:

2
L
L

result:

ok OK!

Test #11:

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

input:

3
XXX

output:

1
XXX

result:

ok OK!

Test #12:

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

input:

3
VVV

output:

3
V
V
V

result:

ok OK!

Test #13:

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

input:

4
MMMM

output:

2
MMM
M

result:

ok OK!

Test #14:

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

input:

4
DDDD

output:

4
D
D
D
D

result:

ok OK!

Test #15:

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

input:

5
CCCCC

output:

2
CCC
CC

result:

ok OK!

Test #16:

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

input:

5
DDDDD

output:

5
D
D
D
D
D

result:

ok OK!

Test #17:

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

input:

6
IIIIII

output:

2
III
III

result:

ok OK!

Test #18:

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

input:

6
VVVVVV

output:

6
V
V
V
V
V
V

result:

ok OK!

Test #19:

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

input:

3
XIX

output:

1
XIX

result:

ok OK!

Test #20:

score: -100
Wrong Answer
time: 0ms
memory: 3836kb

input:

5
XIXIX

output:

2
XIX
XIX

result:

wrong answer Not a split