BestCoder#15-1002

本文介绍了一种复杂的字符串处理算法,该算法能够实现高级别的字符串转换功能,包括将特定指令集转换为二进制代码及逆向转换。通过定义一组预设的操作类型,并使用C++实现,可以有效地进行字符串解析和格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=545

就是规则复杂的字符串处理:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<utility>
#define INF (1<<30)
#define EPS 1e-6
#define PI acos(-1)
#define lowbit(x) ((x) & (-(x)))
#define IDX(l,r) ((l)+(r) | (l)!=(r))
#define ABS(x) ((x)>0?(x):-(x))
#define SET(a,b) memset(a,b,sizeof(a))
#define NN 40
#define MM 10010
inline long long ReadInt()
{
    char ch = getchar();
    long long data = 0;
    while (ch < '0' || ch > '9')
        ch = getchar();
    do
    {
        data = data * 10 + ch - '0';
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9');
    return data;
}
using namespace std;
string code[7];
string convert (int x)
{
    string ret;
    int cnt = 0;
    while (x)
    {
        string temp;
        temp = (x % 2) + '0';
        ret.insert (0, temp);
        x /= 2;
    }
    while (ret.length() < 6) ret.insert (0, "0");
    return ret;
}
int convertBin (string s)
{
    int ret = 0, m = 1;
    for (int i = s.length() - 1; i >= 0; i--)
    {
        ret += (s[i] - '0') * m;
        m *= 2;
    }
    return ret;
}
int convert (string s)
{
    int ret = 0, m = 1;
    for (int i = s.length() - 1; i >= 0; i--)
    {
        ret += (s[i] - '0') * m;
        m *= 10;
    }
    return ret;
}
int main()
{
    string type;
    code[1] = "ADD";
    code[2] = "SUB";
    code[3] = "DIV";
    code[4] = "MUL";
    code[5] = "MOVE";
    code[6] = "SET";
    while (cin >> type)
    {
        string s;
        getchar();
        getline (cin, s);
        if (type == "1")
        {
            string ans = "";
            string op = s.substr (0, s.find (' '));
            for (int i = 1; i <= 6; i++)
                if (code[i] == op)
                {
                    ans += convert (i);
                    //    cout  << ans << endl;
                    break;
                }
            s = s.substr (s.find ('R') + 1);
            op = s.substr (0, s.find (','));
            //  cout<<op<<'$'<<endl;
            string temp = convert (convert (op));
            temp = temp.substr (1);
            ans += temp;
            if (s.find (',') == string::npos) ans += "00000";
            else
            {
                s = s.substr (s.find ('R') + 1);
                temp = convert (convert (s));
                temp = temp.substr (1);
                ans += temp;
            }
            cout << ans << endl;
        }
        else
        {
            string ans = "";
            bool flag = false;
            string op = s.substr (0, 6);
            int temp = convertBin (op);
            if (temp == 6) flag = true;
            if (temp > 6 || temp == 0)
            {
                cout << "Error!" << endl;
                continue;
            }
            ans += code[temp];
            ans += ' ';
            ans += 'R';
            op = s.substr (6, 5);
            temp = convertBin (op);
            if (temp > 31 || temp == 0)
            {
                cout << "Error!" << endl;
                continue;
            }
            if (temp / 10) ans += (temp / 10) + '0';
            ans += (temp % 10) + '0';
            if (!flag)
            {
                ans += ',';
                ans += 'R';
                op = s.substr (11);
                temp = convertBin (op);
                if (temp > 31 || temp == 0)
                {
                    cout << "Error!" << endl;
                    continue;
                }
                if (temp / 10) ans += (temp / 10) + '0';
                ans += (temp % 10) + '0';
            }
            else if (s.substr (11) != "00000")
            {
                cout << "Error!" << endl;
                continue;
            }
            cout << ans << endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值