Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package comp2402a1;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.*;
- public class Part6 {
- /**
- * Your code goes here - see Part0 for an example
- * @param r the reader to read from
- * @param w the writer to write to
- * @throws IOException
- */
- public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
- Set<String> s = new HashSet<>();
- String line;
- for (line = r.readLine(); line != null; line = r.readLine()) {
- if(!s.contains(line)){
- w.println(line);
- }
- String illegalString = "";
- for (int i=line.length() -1;i>=0;i--){
- String currentCharacter = Character.toString(line.charAt(i));
- illegalString = currentCharacter + illegalString;
- s.add(illegalString);
- }
- }
- }
- /*
- while((line=r.readLine()) != null){
- b = false; //reset boolean for each line
- for(String text: s){
- if(text.endsWith(line)) {
- b = true;
- break;
- }
- }
- if (!b) { //if not line from s ends with the new added line, then print it to w
- w.println(line);
- }
- s.add(line); //add line to s
- }//end while
- */
- /**
- * The driver. Open a BufferedReader and a PrintWriter, either from System.in
- * and System.out or from filenames specified on the command line, then call doIt.
- * @param args
- */
- public static void main(String[] args) {
- try {
- BufferedReader r;
- PrintWriter w;
- if (args.length == 0) {
- r = new BufferedReader(new InputStreamReader(System.in));
- w = new PrintWriter(System.out);
- } else if (args.length == 1) {
- r = new BufferedReader(new FileReader(args[0]));
- w = new PrintWriter(System.out);
- } else {
- r = new BufferedReader(new FileReader(args[0]));
- w = new PrintWriter(new FileWriter(args[1]));
- }
- long start = System.nanoTime();
- doIt(r, w);
- w.flush();
- long stop = System.nanoTime();
- System.out.println("Execution time: " + 10e-9 * (stop-start));
- } catch (IOException e) {
- System.err.println(e);
- System.exit(-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement