题目链接: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;
}