I have been working on a problem,
Its simply reading line by line from the console and find the part which contains a pattern and replace it with actual number
Resource: spoj
First i tried using finding the pattern of the text and replacing it with the actual number either by adding or subtracting as per required
My Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class ABSYS {
public static void main(String[] args) throws IOException {
int t;
String[] numArray = new String[2];
String[] numArray2 = new String[2];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(reader.readLine());
while(t > 0) {
String input = reader.readLine();
if(input.isEmpty()) {
continue;
}
numArray = input.split("\\s{1}=\\s{1}");
numArray2 = numArray[0].split("\\s{1}\\+\\s{1}");
Pattern pattern = Pattern.compile("machula");
Matcher matcher = pattern.matcher(numArray[1]);
if(matcher.find()) {
System.out.println(numArray[0] + " = " + (Integer.parseInt(numArray2[0]) + Integer.parseInt(numArray2[1])));
}
else {
matcher = pattern.matcher(numArray2[0]);
if(matcher.find()) {
System.out.println((Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[1])) + " + " + numArray2[1] + " = " + numArray[1]);
}
else {
System.out.println(numArray2[0] + " + " + (Integer.parseInt(numArray[1]) - Integer.parseInt(numArray2[0])) + " = " + numArray[1]);
}
}
t--;
}
}
}
and i got this after implementing it at ideone with 120 test cases
Success time: 0.14 memory: 320832 signal:0
After that, just for testing, i changed completely by eliminating pattern and matcher and used try catch instead
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class ABSYS {
public static void main(String[] args) throws IOException {
int t;
String[] numArray = new String[2];
String[] numArray2 = new String[2];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
t = Integer.parseInt(reader.readLine());
while(t > 0) {
String input = reader.readLine();
if(input.isEmpty()) {
continue;
}
numArray = input.split("\\s{1}=\\s{1}");
numArray2 = numArray[0].split("\\s{1}\\+\\s{1}");
int a = 0, b = 0, c = 0;
try {
c = Integer.parseInt(numArray[1]);
try {
a = Integer.parseInt(numArray2[0]);
System.out.println(a+" + "+(c - a)+" = "+c);
}
catch(NumberFormatException nfe) {
b = Integer.parseInt(numArray2[1]);
System.out.println((c - b)+" + "+b+" = "+c);
}
}
catch(NumberFormatException nfe) {
System.out.println(numArray[0] + " = " + (Integer.parseInt(numArray2[0]) + Integer.parseInt(numArray2[1])));
}
t--;
}
}
}
and i get this as result with the same input
Success time: 0.15 memory: 320832 signal:0
I can't figure it out, why the time got increased in the next implementation even after removing the find method job.
Does it because of try-catch, and if yes, why is it so?
Instead of nesting try catch, we can bring out inner try catch to subsequent to outer catch block. I don't have the exact reason that nesting of try catch would not be a good idea.