Submit | All submissions | Best solutions | Back to list |
NOP - Nop |
Mirko purchased a new microprocessor. Unfortunately, he soon learned that many of his programs that he wrote for his old processor didn't work on the new processor.
Deep inside the technical documentation for both processors, he found an explanation. In order to work faster, the new processor imposes certain constraints on the machine code of programs, constraints that never existed on the previous model.
The machine code of a processor consists of instructions that are executed sequentially. Each instruction uses a byte of memory. Also, instructions can have zero or more parameters, each of which uses an additional byte of memory. In machine code, parameters immediately follow an instruction.
When formatted as text, machine code instructions are uppercase letters, while parameters are lowercase letters. For example:
A b c b B c c C D e f g h
This program consists of four instructions; the first takes three parameters, the second two, the third none and the fourth takes four parameters. The program uses 13 bytes of memory.
The new processor model fetches memory in four-byte chunks so each instruction must start at a memory address that is divisible by four (the first byte in memory is address 0). To achieve that, we can insert NOP (no operation) instructions into the old program, instructions that do nothing and are not limited to memory locations divisible by four. The above program, adapted to run on the new processor, can look like this:
A b c b B c c NOP C NOP NOP NOP D e f g h
The instructions A, B, C and D are now at memory locations 0, 4, 8 and 12, which satisfies the processor's constraints.
Write a program that determines the smallest number of NOP instructions that need to be inserted for the given program to work on the new processor model.
Input
The input contains the machine code of the program written for the old processor model. The program will consist of at most 200 English letters.
The program will always start in an instruction i.e. the first letter in the machine code will be uppercase. If an instruction appears more than once in the machine code, it will always take the same number of parameters.
Output
Output the smallest number of NOP instructions needed to adapt the program for the new processor.
Example
Input Abcd Output 0 Input EaEbFabG Output 5 Input AbcbBccCDefgh Output 4
Score is your source code length. Have fun!
Added by: | Race with time |
Date: | 2009-02-17 |
Time limit: | 1s |
Source limit: | 360B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | COCI 2008/2009 - Croatian Regional |
hide comments
|
|||||
2012-01-19 09:38:59 abpdora
my problem id is XXXXXXX can you tell me what could i be missing it works now witha tweak . i guess the i/p has charaters apart from [A-Za-z] Last edit: 2012-01-19 09:51:05 |
|||||
2011-06-05 07:01:23 Santiago Palacio
Dont scan with EOF! just scan once, there's only one test case that runs several times (i guess... but scan ONCE) |
|||||
2011-06-05 01:23:50 Santiago Zubieta
What if a instruction has more than 4 parameters, do you need to fill it with NOPs until the next position that X%4=0? EDIT: got it! yeah, AabcdB means you have to fill NOP until B reaches the 8th position. Last edit: 2011-06-05 02:38:32 |
|||||
2011-04-04 17:16:20 Sudhanshu Gupta
Is there only single test case.. |
|||||
2010-12-06 18:03:58 Sanchit Abrol
i'm getting wrong answer when i submit my solution but the test cases are showing correct answer on my Dev-C++. please help. can there be a formatting problem? i am reading till EOF and outputting once. |
|||||
2010-11-30 09:53:21 praveen
what if the first instruction has 4 parameters.. like Aabcd.. is this the answer Aabcd nop nop nop? |
|||||
2009-02-19 22:36:49 Miorel-Lucian Palii
Yep, it's true, this seems to have non-Unix newlines. |
|||||
2009-02-19 22:00:17 Miorel-Lucian Palii
Hmm, are there extra characters at the end of a line? |
|||||
2009-02-19 18:27:23 numerix
pdf printing doesn't work Edit: Now it works ... Last edit: 2009-08-15 19:55:52 |