PKU 2685 Numeral System

  • やるだけ
int table[0x80];

string encode(int n) {
	static const string letters = "ixcm";
	static const string digits = "0123456789";
	string s;
	for (int i = 0; i < 4; ++i) {
		int value = n % 10;
		n /= 10;
		if (value) {
			s = letters.substr(i, 1) + s;
			if (value > 1) {
				s = digits.substr(value, 1) + s;
			}
		}
	}
	return s;
}

int decode(const string& s) {
	int index = 0;
	int value = 0;
	while (index < s.size()) {
		int pre = 1;
		if (isdigit(s[index])) {
			pre = s[index++] - '0';
		}

		pre *= table[s[index++]];
		value += pre;
	}
	return value;
}

int main() {
	table['m'] = 1000;
	table['c'] = 100;
	table['x'] = 10;
	table['i'] = 1;

	int n;
	cin >> n;
	REP(testCaseIndex, n) {
		string a, b;
		cin >> a >> b;
		cout << encode(decode(a) + decode(b)) << endl;
	}
}