Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. map<string, int> decm;
  4. int NUM[13] = {
  5. 1, 4, 5, 9,
  6. 10, 40, 50, 90,
  7. 100, 400, 500, 900,
  8. 1000
  9. };
  10. string SYM[13] = {
  11. "I", "IV", "V", "IX",
  12. "X", "XL", "L", "XC",
  13. "C", "CD", "D", "CM",
  14. "M"
  15. };
  16.  
  17. void init() {
  18. for (int i = 0; i < 13; i++) {
  19. int num = NUM[i];
  20. string sym = SYM[i];
  21. decm[sym] = num;
  22. }
  23. }
  24.  
  25. // decode
  26. int dec(string s) {
  27. int ret = 0;
  28. for (size_t i = 0; i < s.size(); i++) {
  29. string f2 = s.substr(i, 2); // first 2 chars
  30. string f1 = s.substr(i, 1); // first 1 char
  31. if (i + 1 < s.size() && decm.find(f2) != decm.end()) {
  32. ret += decm[f2];
  33. i++; // skip 2 chars
  34. } else {
  35. ret += decm[f1];
  36. }
  37. }
  38. return ret;
  39. }
  40.  
  41. // encode
  42. string enc(int x) {
  43. if (x == 0) {
  44. return "ZERO";
  45. }
  46. string ret = "";
  47. for (int i = 12; i >= 0; i--) {
  48. int num = NUM[i];
  49. string sym = SYM[i];
  50. while (x >= num) {
  51. ret += sym;
  52. x -= num;
  53. }
  54. }
  55. return ret;
  56. }
  57.  
  58. int main() {
  59. init();
  60. string a, b;
  61. while (cin >> a && a != "#") {
  62. cin >> b;
  63. int ans = abs(dec(a) - dec(b));
  64. cout << enc(ans) << endl;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement