I was doing a USACO contest recently. I had reached the correct algorithm for an answer, but I only got 7 test cases out of 10, with the remaining three showing up as "time limit exceeded". I searched up on GeeksforGeeks and javaTpoint on why this happens, and those two sites told me that either inefficient algorithms or the usage of Scanner instead of BufferedReader.
Throughout my Java experience (2 years), I have been using Scanner and am unfamiliar with anything else. Despite combing through several pages, I am unable to find a satisfactory answer to what methods in Scanner translate to which methods in BufferedReader, as I would like to use it moving forward. As I am unfamiliar with BufferedReader, I may be asking the wrong question. The methods I need are the BufferedReader equivalents to nextLine() and nextInt(). What are the use cases for each class, and how would I achieve the same function these methods do in Scanner using BufferedReader?
There are two methods in BufferedReader to read from any data stream.
int read(char[] cbuf, int off, int len)
It is used for reading characters into a portion of an array.
String readLine()
It is used for reading a line of text.
BufferedReader is more useful when you want to read string type of data for that you can use readLine() method. For custom type like int, long etc preferred to use Scanner only. So there is no equivalents method for nextInt(), nextLong() in BufferedReader.
Related
Hey I'm currently trying to emulate an old gaming console. I'm run into a little problem that I do not know how to fix.
More specific, I'm doing a emulator for a GBA. But to make it a little more challenging, I have decided to emulate the games as external devices with memory, possible extra CPUs and such. Just like they are in the real world. To do this I need to create a self contained executable file, where there is allocated some fix size spare memory to save data like saved games.
Is this possible? I know this is uncommon and bad habit. I don't care, it is like people saying DON'T USE GOTO in C, the instruction are there to be used, and I use it with good effect an no headache, and no weird behaviour, it just takes a moment of planning, and maybe a course in compiler implementation to know how.
The language are not important, C will do, but if .net supported it I'll prefer that.
You can take the approach used by some self-extracting archives. Append a marker GUID and a data block after the compiled executable
(executable file) + (marker GUID) + (data area)
In Windows for example, using a command box:
type "c:\vsprojects\my-gba\release\my-gba.exe" "c:\data-files\guid.txt" "c:\data-files\save-data-area.txt" > "c:\data-files\gba-combo.exe"
In Linux you'd cat the 3 pieces together into a new file to form your executable-with-data.
To find the start of the data area, you search for the GUID in the EXE and then seek past it.
There is a very slight chance the GUID could occur naturally in your code, but the odds of that become extremely low the longer the GUID gets. You can use an online GUID generator and combine multiple strings in the GUID text file for example
731056dd-1dd7-4f46-b90b-2ad623198404 a2d0cd76-f8fc-4bcf-8a15-80ca4d9b205f
You do need to make sure you don't write your code in a way that embeds the GUID as a string constant earlier in your code, before the appended marker.
BAD:
const char* foo = "731056dd-1dd7-4f46-b90b-2ad623198404 a2d0cd76-f8fc-4bcf-8a15-80ca4d9b205f"
-- code that seeks for foo ---
OK:
const char* arr[] = { "731056dd-1dd7-4f46-b90b-2ad623198404", "BREAK-MATCH", "a2d0cd76-f8fc-4bcf-8a15-80ca4d9b205f" };
-- code sets foo from arr [0], " ", [2] then seeks for foo ---
I've seen several questions/answers here that suggest the best way to get a string representation of an integer in Objective-C is to use [NSString stringWithFormat:#"%d", x]. I'm afraid the C/C++ programmer in me is having a hard time believing I want to bring all the formatting code into play for such a simple task. That is, I assume stringWithFormat needs to parse through the format string looking for all the different type specifiers, field widths, and options that I could possibly use, then it has to be able to interpret that variable length list of parameters and use the format specifier to coerce x to the appropriate type, then go through a lengthy process of conversion, accounting for signed/unsigned values and negation along the way.
Needless to say in C/C++ I could simply use itoa(x) which does exactly one thing and does it extremely efficiently.
I'm not interested in arguing the relative merits of one language over another, but rather just asking the question: is the incredibly powerful [NSString stringWithFormat:#"%d", x] really the most efficient way to do this very, very simple task in Objective-C? Seems like I'm cracking a peanut with a sledge hammer.
You could use itoa() followed by any of +[NSString stringWithUTF8String:] -[NSString initWithBytes:length:encoding: or +[NSString stringWithCString:encoding:] if it makes you feel better, but I wouldn't worry about it unless you're sure this is a performance problem.
You could also use description method. It boxes it as NSNumber and converts to a NSString.
int intVariable = 1;
NSString* stringRepresentation = [#(intVariable) description];
A comment buried in some C++ code in the SSCLI claims, referring to the unmanaged internal implementation of String.Chars property:
This method is not actually used. JIT will generate code for indexer method on string class.
So...what magical code is this? I understand the whole point of jitters is that they produce different code in different situations. But at the very least, for a modern x64 Windows 7+ platform, how might the/a jitter accomplish this? Or is that truly secret sauce?
Additional details
A while ago I was looking for the fastest way to iterate through individual characters in a string in C#.
It turned out the fastest way without resorting to unsafe code or duplicating the contents (via ToCharArray())
was the built-in string indexer, which is actually a call to the String.Chars property. Right in my original
question I asked if anyone had insight into how the indexer actually worked, but despite bumps from both Skeet and
Lippert, I didn't get any responses on that. So I decided to dig into it myself:
Stop 1: mscorlib
By examining mscorlib.dll with ildasm, we can see that String::get_Chars(int32 index) is just an internalcall pointer (plus an attribute):
.method public hidebysig specialname instance char
get_Chars(int32 index) cil managed internalcall
{
.custom instance void System.Security.SecuritySafeCriticalAttribute::.ctor() = ( 01 00 00 00 )
} // end of method String::get_Chars
As noted in the documentation for the MethodImplOptions enumeration, "An internal call is a call to a method that is implemented within the common language runtime itself." Both a 2004 MSDN Magazine article and an SO post indicate that the mapping of internalcall names to unmanaged implementations can be found in ecall.cpp within the Shared Source CLI.
Stop 2: ecapp.cpp
Searching an online copy of ecall.cpp reveals that get_Chars is implemented by COMString::GetCharAt:
FCIntrinsic("get_Chars", COMString::GetCharAt, CORINFO_INTRINSIC_StringGetChar)
Stop 3: comstring.cpp
comstring.cpp does indeed contain an implementation of GetCharAt, starting at line 1219. Except, it's preceded by this comment:
/*==================================GETCHARAT===================================
**Returns the character at position index. Thows IndexOutOfRangeException as
**appropriate.
**This method is not actually used. JIT will generate code for indexer method on string class.
**
==============================================================================*/
First of all, see Hans Passant's comment for the critical bit.
In early .NET (CLR 1 and 2), the CLR had considerable special support for String and StringBuilder types. In fact, the two types worked so closely together, that StringBuilder.ToString was not copying the actual characters anywhere, and the string indexer was still fetching the characters from that same memory location, using special jitter support. I assume that jitter support for String.Chars was originally necessary to avoid passing the index integer via stack, but the jitter seems to have improved since then.
.NET 4 comes with a different implementation of StringBuilder (ropes) that no longer is tied to how String is handled. (It has to copy during ToString, but has much faster appends.) After these changes,
StringBuilder indexer is drammatically slowed down to O(log n) on large strings. See here. It is never inlined, not even on short strings.
String indexer still uses (unpublished) special jitter support. I would expect this one to be basically inlined away into a shift, addition and a memory fetch, or something even faster that the nearest loop would allow.
What are the main existing approaches to hide the value of literals in code, so that they are not easily traced with just an hexdumper or a decompiler?
For example, instead of coding this:
static final int MY_VALUE = 100;
We could have:
static final int MY_VALUE = myFunction1();
private int myFunction1(){
int i = 23;
i += 8 << 4;
for(int j = 0; j < 3; j++){
i-= (j<<1);
}
return myFunction2(i);
}
private int myFunction2(int i){
return i + 19;
}
That was just an example of what we're trying to do. (Yes, I know, the compiler may optimize it and precalculate the constant).
Disclaimer: I know this will not provide any aditional security at all, but it makes the code more obscure (or interesting) to reverse-engineer. The purpose of this is just to force the attacker to debug the program, and waste time on it. Keep in mind that we're doing it just for fun.
Since you're trying to hide text, which will be visible in the simple dump of the program, you can use some kind of simple encryption to obfuscate your program and hide that text from prying eyes.
Detailed instuctions:
Visit ROT47.com and encode your text online. You can also use this web site for a more generic ROTn encoding.
Replace contents of your string constants with the encoded text.
Use the decoder in your code to transform the text back into its original form when you need it. ROT13 Wikipedia article contains some notes about implementation, and here is Javascript implementation of ROTn on StackOverflow. It is trivial to adapt it to whatever language you're using.
Why use ROT47 which is notoriously weak encryption?
In the end, your code will look something like this:
decryptedData = decryptStr(MY_ENCRYPTED_CONSTANT)
useDecrypted(decryptedData)
No matter how strong your cypher, anybody equipped with a debugger can set a breakpoint on useDecrypted() and recover the plaintext. So, strength of the cypher does not matter. However, using something like Rot47 has two distinct advantages:
You can encode your text online, no need to write a specialized program to encode your text.
Decryption is very easy to implement, so you don't waste your time on something that does not add any value to your customers.
Anybody reading your code (your coworker or yourself after 5 years) will know immediately this is not a real security, but security by obscurity.
Your text will still appear as gibberish to anyone just prying inside your compiled program, so mission accomplished.
Run some game of life variant for a large number of iterations, and then make control flow decisions based on the final state vector.
If your program is meant to actually do something useful, you could have your desired branches planned ahead of time and choose bits of the state vector to suit ("I want a true here, bit 17 is on, so make that the condition..")
You could also use some part of compiled code as data, then modify it a little. This would be hard to do in a program executed by virtual machine, but is doable in languages like asm or c.
Any programming language. I'm interested in knowing what top 5 methods in a string class are used on data manipulations. Or what top 5 methods does one need to know to be able to handle data manipulation. I know probably all the methods together should be used, but I'm interested to see the 5 most common methods people use.
Thanks for your time.
I'd say
String.Format()
String.Split()
String.IndexOf()
String.Substring()
String.ToUpper()
Adam's top 5 are pretty much mine. I might replace IndexOf() with Trim(); I use this EVERY TIME I get a value from the user. String.Compare() using the IgnoreCase values of the StringComparison enumeration would replace most of the uses I've seen for ToUpper().
Format, HEAVILY used in logs and other user messages (far more efficient for templated messages than a bunch of += statements or a StringBuilder()). Split and Substring, ditto, especially in file processing.
Adam's + KeithS. But don't forget the under the hood calls to
String.hashCode(),
String.equals(String rhs)
and its ilk.