#include <iostream> #include <string> using namespace std; int main() { string num1, num2, result; int tmp1=0, tmp2=0, tmp=0; int count=0, count2=0; int i, j; cin >> num1 >> num2; count = num1.length(); count2 = num2.length(); for(i=0; i<count; i++) { if(num1[i] == 'm') { if( (i-1 >= 0) && (num1[i-1] != 'm') && (num1[i-1] != 'c') && (num1[i-1] != 'x') &&(num1[i-1] != 'i') ){ tmp1 = tmp1 + (((int)num1[i-1]-48) * 1000); } else { tmp1 = tmp1 + 1000; } } else if(num1[i] == 'c') { if( (i-1 >= 0) && (num1[i-1] != 'm') && (num1[i-1] != 'c') && (num1[i-1] != 'x') &&(num1[i-1] != 'i') ) { tmp1 = tmp1 + (((int)num1[i-1]-48) * 100); } else { tmp1 = tmp1 + 100; } } else if(num1[i] == 'x') { if( (i-1 >= 0) && (num1[i-1] != 'm') && (num1[i-1] != 'c') && (num1[i-1] != 'x') &&(num1[i-1] != 'i') ) { tmp1 = tmp1 + (((int)num1[i-1]-48) * 10); } else { tmp1 = tmp1 + 10; } } else if(num1[i] == 'i') { if( (i-1 >= 0) && (num1[i-1] != 'm') && (num1[i-1] != 'c') && (num1[i-1] != 'x') &&(num1[i-1] != 'i') ) { tmp1 = tmp1 + (((int)num1[i-1]-48)); } else { tmp1 = tmp1 + 1; } } } for(j=0; j<count2; j++) { if(num2[j] == 'm') { if( (j-1 >= 0) && (num2[j-1] != 'm') && (num2[j-1] != 'c') && (num2[j-1] != 'x') &&(num2[j-1] != 'i') ){ tmp2 = tmp2 + (((int)num2[j-1]-48) * 1000); } else { tmp2 = tmp2 + 1000; } } else if(num2[j] == 'c') { if( (j-1 >= 0) && (num2[j-1] != 'm') && (num2[j-1] != 'c') && (num2[j-1] != 'x') &&(num2[j-1] != 'i') ) { tmp2 = tmp2 + (((int)num2[j-1]-48) * 100); } else { tmp2 = tmp2 + 100; } } else if(num2[j] == 'x') { if( (j-1 >= 0) && (num2[j-1] != 'm') && (num2[j-1] != 'c') && (num2[j-1] != 'x') &&(num2[j-1] != 'i') ) { tmp2 = tmp2 + (((int)num2[j-1]-48) * 10); } else { tmp2 = tmp2 + 10; } } else if(num2[j] == 'i') { if( (j-1 >= 0) && (num2[j-1] != 'm') && (num2[j-1] != 'c') && (num2[j-1] != 'x') &&(num2[j-1] != 'i') ) { tmp2 = tmp2 + (((int)num2[j-1]-48)); } else { tmp2 = tmp2 + 1; } } } //cout << tmp1 << " " <<tmp2<<endl; tmp = tmp1 + tmp2; //cout <<tmp <<endl; if((tmp/1000) !=0){ if((tmp/1000) == 1) { result += 'm'; } else { result += (tmp/1000) + 48; result += 'm'; } } if((tmp%1000)/100 != 0) { if((tmp%1000)/100 == 1) result += 'c'; else if((tmp%1000)/100 != 1) { result += (tmp%1000)/100 + 48; result += 'c'; } } if((tmp%1000%100)/10 != 0) { if((tmp%1000%100)/10 == 1) result += 'x'; else if((tmp%1000%100)/10 != 1) { result += (tmp%1000%100)/10 + 48; result += 'x'; } } if((tmp%10) == 1){ result += 'i'; } else if(tmp%10 != 0) { result += tmp%10 + 48; result += 'i'; } cout << result; return 0; } |