Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- map<string, int> decm;
- int NUM[13] = {
- 1, 4, 5, 9,
- 10, 40, 50, 90,
- 100, 400, 500, 900,
- 1000
- };
- string SYM[13] = {
- "I", "IV", "V", "IX",
- "X", "XL", "L", "XC",
- "C", "CD", "D", "CM",
- "M"
- };
- void init() {
- for (int i = 0; i < 13; i++) {
- int num = NUM[i];
- string sym = SYM[i];
- decm[sym] = num;
- }
- }
- // decode
- int dec(string s) {
- int ret = 0;
- for (size_t i = 0; i < s.size(); i++) {
- string f2 = s.substr(i, 2); // first 2 chars
- string f1 = s.substr(i, 1); // first 1 char
- if (i + 1 < s.size() && decm.find(f2) != decm.end()) {
- ret += decm[f2];
- i++; // skip 2 chars
- } else {
- ret += decm[f1];
- }
- }
- return ret;
- }
- // encode
- string enc(int x) {
- if (x == 0) {
- return "ZERO";
- }
- string ret = "";
- for (int i = 12; i >= 0; i--) {
- int num = NUM[i];
- string sym = SYM[i];
- while (x >= num) {
- ret += sym;
- x -= num;
- }
- }
- return ret;
- }
- int main() {
- init();
- string a, b;
- while (cin >> a && a != "#") {
- cin >> b;
- int ans = abs(dec(a) - dec(b));
- cout << enc(ans) << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement