hdu 5083 Instruction(Bestcoder Round #15)

Instruction

                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768
K (Java/Others)

Total Submission(s): 327    Accepted Submission(s): 94

Problem Description

Nowadays, Jim Green has produced a kind of computer called JG. In his computer, the instruction is represented by binary code. However when we code in this computer, we use some mnemonic symbols. For example, ADD R1, R2 means to add the number in register R1
and R2, then store the result to R1. But this instruction cannot be execute directly by computer, before this instruction is executed, it must be changed to binary code which can be executed by computer. Each instruction corresponds to a 16-bit binary code.
The higher 6 bits indicates the operation code, the middle 5 bits indicates the destination operator, and the lower 5 bits indicates the source operator. You can see Form 1 for more details.

15 operation
code(6 bits)109destination
operator code(5 bits)54source
operator code(5 bits)0Form
1

In JG system there are 6 instructions which are listed in Form 2.

instructionADD
Ra,RbSUB Ra,RbDIV
Ra,RbMUL Ra,RbMOVE
Ra,RbSET RafunctionAdd
the number in register Ra and Rb, then store the result to Ra.Subtract
the number in register Ra to Rb, then store the result to Ra.Divide
the number in register Ra by Rb, then store the result to Ra.Mulplicate
the number in register Ra and Rb, then store the result to Ra.Move
the number in register Rb to Ra.Set 0 to Ra.Form
2

Operation code is generated according to Form 3.

OperationADDSUBDIVMULMOVESETOperation
code000001000010000011000100000101000110Form
3

Destination operator code and source operator code is the register code of the register which is related to.

There are 31 registers in total. Their names are R1,R2,R3…,R30,R31. The register code of Ri is the last 5 bits of the number of i in the binary system. For eaxample the register code of R1 is 00001, the register code of R2 is 00010, the register code of R7
is 00111, the register code of R10 is 01010, the register code of R31 is 11111.

So we can transfer an instruction into a 16-bit binary code easyly. For example, if we want to transfer the instruction ADD R1,R2, we know the operation is ADD whose operation code is 000001, destination operator code is 00001 which is the register code of
R1, and source operator code is 00010 which is the register code of R2. So we joint them to get the 16-bit binary code which is 0000010000100010.

However for the instruction SET Ra, there is no source register, so we fill the lower 5 bits with five 0s. For example, the 16-bit binary code of SET R10 is 0001100101000000

You are expected to write a program to transfer an instruction into a 16-bit binary code or vice-versa.

Input

Multi test cases (about 50000), every case contains two lines.

First line contains a type sign, ‘0’ or ‘1’.

‘1’ means you should transfer an instruction into a 16-bit binary code;

‘0’ means you should transfer a 16-bit binary code into an instruction.

For the second line.

If the type sign is ‘1’, an instruction will appear in the standard form which will be given in technical specification;

Otherwise, a 16-bit binary code will appear instead.

Please process to the end of file.

[Technical Specification]

The standard form of instructions is

ADD Ra,Rb

SUB Ra,Rb

DIV Ra,Rb

MUL Ra,Rb

MOVE Ra,Rb

SET Ra

which are also listed in the Form 2.

1≤a,b≤31

There is exactly one space after operation, and exactly one comma between Ra and Rb other than the instruction SET Ra. No other character will appear in the instruction.

Output

For type ‘0’,if the 16-bit binary code cannot be transferred into a instruction according to the description output “Error!” (without quote), otherwise transfer the 16-bit binary code into instruction and output the instruction in the standard form in a single
line.

For type ‘1’, transfer the instruction into 16-bit binary code and output it in a single line.

Sample Input

1
ADD R1,R2
0
0000010000100010
0
1111111111111111

Sample Output

0000010000100010
ADD R1,R2
Error!

模拟一下就好了。Ra,Rb中的1<=a,b<=31,解码时要判断是否符合条件,我还以为是给定的条件呢,wa了好几

发。

ps:在这题上感受到了Bestcoder深深的恶意,就是不让我过两题,我的rating啊,已经跌的惨不忍睹了%>_<%,

上次差几秒剪枝就加上去了,这次竟然TLE,用g++交的,用C++交78ms过。g++与C++差别怎么就这么大啊。

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
string s;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)
        {
            cin>>s;
            if(s.size()>16)
            {
                printf("Error!\n");
                continue;
            }
            int a=0,b=0,c=0;
            for(int i=5; i>=0; i--)
            {
                if(s[i]-'0'==1)
                {
                    a+=(1<<(5-i));
                }
            }
            for(int i=10; i>=6; i--)
            {
                if(s[i]-'0'==1)
                {
                    b+=(1<<(10-i));
                }
            }
            for(int i=15; i>=11; i--)
            {
                if(s[i]-'0'==1)
                {
                    c+=(1<<(15-i));
                }
            }
            if(a==1&&b>0&&c>0)
            {
                printf("ADD R%d,R%d\n",b,c);
            }
            else if(a==2&&b>0&&c>0)
            {
                printf("SUB R%d,R%d\n",b,c);
            }
            else if(a==3&&b>0&&c>0)
            {
                printf("DIV R%d,R%d\n",b,c);
            }
            else if(a==4&&b>0&&c>0)
            {
                printf("MUL R%d,R%d\n",b,c);
            }
            else if(a==5&&b>0&&c>0)
            {
                printf("MOVE R%d,R%d\n",b,c);
            }
            else if(a==6&&b>0&&c==0)
            {
                printf("SET R%d\n",b);
            }
            else
                printf("Error!\n");
        }
        else
        {
            char s1,s2,temp,temp2;
            int x,y;
            cin>>s;
            int a[10];
            int b[10];
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            if(s=="ADD")
            {
                scanf("%c%c%d%c%c%d",&temp2,&s1,&x,&temp,&s2,&y);
                // printf("%d  %d\n",x,y);
                printf("000001");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=4; i>=0; i--)
                {
                    if(y%2)
                        b[i]=1;
                    else
                        b[i]=0;
                    y=y/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("%d",b[i]);
                printf("\n");
            }
            else if(s=="SUB")
            {
                scanf("%c%c%d%c%c%d",&temp2,&s1,&x,&temp,&s2,&y);
                // printf("%d  %d\n",x,y);
                printf("000010");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=4; i>=0; i--)
                {
                    if(y%2)
                        b[i]=1;
                    else
                        b[i]=0;
                    y=y/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("%d",b[i]);
                printf("\n");
            }
            else if(s=="DIV")
            {
                scanf("%c%c%d%c%c%d",&temp2,&s1,&x,&temp,&s2,&y);
                // printf("%d  %d\n",x,y);
                printf("000011");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=4; i>=0; i--)
                {
                    if(y%2)
                        b[i]=1;
                    else
                        b[i]=0;
                    y=y/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("%d",b[i]);
                printf("\n");
            }
            else if(s=="MUL")
            {
                scanf("%c%c%d%c%c%d",&temp2,&s1,&x,&temp,&s2,&y);
                // printf("%d  %d\n",x,y);
                printf("000100");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=4; i>=0; i--)
                {
                    if(y%2)
                        b[i]=1;
                    else
                        b[i]=0;
                    y=y/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("%d",b[i]);
                printf("\n");
            }
            else if(s=="MOVE")
            {
                scanf("%c%c%d%c%c%d",&temp2,&s1,&x,&temp,&s2,&y);
                // printf("%d  %d\n",x,y);
                printf("000101");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=4; i>=0; i--)
                {
                    if(y%2)
                        b[i]=1;
                    else
                        b[i]=0;
                    y=y/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("%d",b[i]);
                printf("\n");
            }
            else if(s=="SET")
            {
                scanf("%c%c%d",&temp2,&s1,&x);
                // printf("%d  %d\n",x,y);
                printf("000110");
                for(int i=4; i>=0; i--)
                {
                    if(x%2)
                        a[i]=1;
                    else
                        a[i]=0;
                    x=x/2;
                }
                for(int i=0; i<=4; i++)
                    printf("%d",a[i]);
                for(int i=0; i<=4; i++)
                    printf("0");
                printf("\n");
            }
        }
    }
    return 0;
}

时间: 2024-10-06 00:36:12

hdu 5083 Instruction(Bestcoder Round #15)的相关文章

BestCoder Round#15 1001-Love

http://acm.hdu.edu.cn/showproblem.php?pid=5082 Love Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 64    Accepted Submission(s): 51 Problem Description There is a Love country with many couples

[ACM] HDU 5083 Instruction (模拟)

Instruction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 347    Accepted Submission(s): 101 Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer

HDU 5904 - LCIS (BestCoder Round #87)

HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式: dp[a[i]] = max(dp[a[i]], dp[a[i]-1] + 1); 发现其实可以简化为 dp[a[i]] = dp[a[i]-1] + 1:因为计算过程中dp[a[i]]不会降低 对两个序列都求一遍,然后取两者最小值的最大值 1 #include <cstdio> 2 #inc

hdu 5748 Bellovin(BestCoder Round #84——最长递增子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748 Bellovin Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 929    Accepted Submission(s): 421 Problem Description Peter has a sequence a1,a2,

HDU 5666 Segment——BestCoder Round #80

Segment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Silen August does not like to talk with others.She like to find some interesting probl

hdu 5082 Love(Bestcoder Round #15)

Love Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 78    Accepted Submission(s): 57 Problem Description There is a Love country with many couples of Darby and Joan in it. In order to commemor

BestCoder Round#15 1002-Instruction

http://acm.hdu.edu.cn/showproblem.php?pid=5083 官方题解——> 1002 Instruction 先考虑编码,首先找到operation对应的编码,如果是SET就找后面的一个R后面跟着的数字a,令b=0,否则找后面第一个R后面的数字当作a,第二个R后面的数字当作b,最后依次输出operation二进制编码,a, b的二进制编码. 再说解码,先将前6位,中间5位和后面5位转化成十进制记为oid, a, b.如果oid<1||oid>6就是Err

hdu 4859 海岸线 Bestcoder Round 1

http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格线满足两侧分别是海洋和陆地 这道题很神 首先考虑一下,什么情况下能够对答案做出贡献 就是相邻的两块不一样的时候 这样我们可以建立最小割模型,可是都说是最小割了 无法求出最大的不相同的东西 所以我们考虑转化,用总的配对数目 - 最小的相同的对数 至于最小的相同的对数怎么算呢? 我们考虑这样的构造方法:

HDU 5083 Instruction(字符串处理)

Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer, the instruction is represented by binary code. However when we code in this computer, we use some mnemonic symbols. For example, ADD R1, R2 means to a