Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
4
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

string convert(string s) {
    string chal = "w1z3qmbi";
    unordered_set<char> st;
    for (int i = 0; i < chal.size(); i++)
        st.insert(chal[i]);
    
    string ans = "";
    for (int i = 0; i < s.size(); i++) {
        if (st.count(s[i])) ans += "--" + string(1, s[i]) + "--";
        else ans.push_back(s[i]);
    }
    return ans;
}

string solve(string s) {
    unordered_map<int, string> mp {
        {0, "zero"},
        {1, "one"},
        {2, "two"},
        {3, "three"},
        {4, "four"},
        {5, "five"},
        {6, "six"},
        {7, "seven"},
        {8, "eight"},
        {9, "nine"},
    };
    vector<string> v {
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
    };
    int num = 0;
    int sign = 1;
    int sum = 0;
    
    while (s.size() > 0) {
        if (s.rfind("plus", 0) == 0) {
            sum += num * sign;
            num = 0;
            sign = 1;
            s = s.substr(4);
        } else if (s.rfind("minus", 0) == 0) {
            sum += num * sign;
            num = 0;
            sign = -1;
            s = s.substr(5);
        } else {
            for (int i = 0; i < v.size(); i++) {
                if (s.rfind(v[i], 0) == 0) {
                    num = num * 10 + i;
                    s = s.substr(v[i].size());
                    break;
                }
            }
        }
    }
    
    sum += num * sign;
    string output = "";
    if (sum < 0) {
        output += "negative";
        sum *= -1;
    }
    
    string sum_str = to_string(sum);
    for (int i = 0; i < sum_str.size(); i++) {
        output += mp[sum_str[i] - '0'];
    }
    
    return convert(output);
}