add fastscanner
This commit is contained in:
@@ -1,49 +1,81 @@
|
|||||||
package reverse;
|
package reverse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.io.*;
|
||||||
import java.util.Scanner;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Reverse {
|
public class Reverse {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws IOException {
|
||||||
Scanner sc = new Scanner(System.in);
|
FastScanner sc = new FastScanner();
|
||||||
ArrayList<ArrayList<String>> lines = new ArrayList<>();
|
int[][] lines = new int[8][];
|
||||||
|
int linesCount = 0;
|
||||||
|
|
||||||
while (sc.hasNextLine()) {
|
while (sc.hasNextLine()) {
|
||||||
ArrayList<String> line = new ArrayList<>();
|
int[] line = new int[8];
|
||||||
StringBuilder builder = new StringBuilder();
|
int count = 0;
|
||||||
String lineString = sc.nextLine();
|
|
||||||
|
|
||||||
for (char c : lineString.toCharArray()) {
|
while (sc.hasNextInt()) {
|
||||||
if (!Character.isWhitespace(c)) {
|
if (count >= line.length) {
|
||||||
builder.append(c);
|
line = Arrays.copyOf(line, line.length * 2);
|
||||||
} else {
|
|
||||||
if (!builder.toString().isEmpty()) {
|
|
||||||
line.add(builder.toString());
|
|
||||||
}
|
}
|
||||||
builder = new StringBuilder();
|
line[count++] = sc.nextInt();
|
||||||
}
|
}
|
||||||
|
sc.nextLine();
|
||||||
|
|
||||||
|
line = Arrays.copyOf(line, count);
|
||||||
|
|
||||||
|
if (linesCount >= lines.length) {
|
||||||
|
lines = Arrays.copyOf(lines, lines.length * 2);
|
||||||
}
|
}
|
||||||
if (!builder.toString().isEmpty()) {
|
lines[linesCount++] = line;
|
||||||
line.add(builder.toString());
|
|
||||||
}
|
|
||||||
lines.add(line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sc.close();
|
PrintWriter out = new PrintWriter(System.out);
|
||||||
|
for (int i = linesCount - 1; i >= 0; i--) {
|
||||||
|
int[] line = lines[i];
|
||||||
|
for (int j = line.length - 1; j >= 0; j--) {
|
||||||
|
if (j < line.length - 1) out.print(" ");
|
||||||
|
out.print(line[j]);
|
||||||
|
}
|
||||||
|
out.println();
|
||||||
|
}
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class FastScanner {
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
String line = null;
|
||||||
|
int pos = 0;
|
||||||
|
|
||||||
|
boolean hasNextLine() throws IOException {
|
||||||
|
if (line != null && pos < line.length()) return true;
|
||||||
|
line = br.readLine();
|
||||||
|
pos = 0;
|
||||||
|
return line != null;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = lines.size() - 1; i >= 0; i--) {
|
boolean hasNextInt() {
|
||||||
if (lines.get(i).isEmpty()) {
|
if (line == null) return false;
|
||||||
System.out.println();
|
while (pos < line.length() && Character.isWhitespace(line.charAt(pos))) pos++;
|
||||||
}
|
return pos < line.length();
|
||||||
for (int j = lines.get(i).size() - 1; j >= 0; j--) {
|
|
||||||
if (j > 0) {
|
|
||||||
System.out.print(lines.get(i).get(j) + " ");
|
|
||||||
} else {
|
|
||||||
System.out.println(lines.get(i).get(j));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int nextInt() {
|
||||||
|
while (pos < line.length() && Character.isWhitespace(line.charAt(pos))) pos++;
|
||||||
|
int start = pos;
|
||||||
|
boolean negative = line.charAt(pos) == '-';
|
||||||
|
if (negative) pos++;
|
||||||
|
|
||||||
|
while (pos < line.length() && Character.isDigit(line.charAt(pos))) pos++;
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
for (int i = negative ? start + 1 : start; i < pos; i++) {
|
||||||
|
result = result * 10 + (line.charAt(i) - '0');
|
||||||
|
}
|
||||||
|
return negative ? -result : result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void nextLine() {
|
||||||
|
pos = line.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
2
java/reverse/test.dat
Normal file
2
java/reverse/test.dat
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
1 2
|
||||||
|
3
|
||||||
Reference in New Issue
Block a user