Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.01 KB | None | 0 0
  1. package pack;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class FindWords {
  12.     private List<String> data = null, words = null;
  13.     private StringBuffer dataBufferWE = null, dataBufferNS = null, dataBufferNE = null, dataBufferWN = null, dataBufferAll = null;
  14.     private List<String> positionWE = null, positionNS = null, positionNE = null, positionWN = null, positionAll = null, usedCoordinates = null;
  15.     private int row = -1, column = -1;
  16.  
  17.     public FindWords() {
  18.         data = new ArrayList<String>();
  19.         words = new ArrayList<String>();
  20.         dataBufferWE = new StringBuffer();
  21.         dataBufferNS = new StringBuffer();
  22.         dataBufferNE = new StringBuffer();
  23.         dataBufferWN = new StringBuffer();
  24.         dataBufferAll = new StringBuffer();
  25.         positionWE = new ArrayList<String>();
  26.         positionNS = new ArrayList<String>();
  27.         positionNE = new ArrayList<String>();
  28.         positionWN = new ArrayList<String>();
  29.         positionAll = new ArrayList<String>();
  30.         usedCoordinates = new ArrayList<String>();
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         FindWords findWords = new FindWords();
  35.         String result = findWords.parseFile(args[0]);
  36.         System.out.println(result);
  37.     }
  38.  
  39.     public String parseFile(String fileName) {
  40.         try {
  41.             BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
  42.             String line = reader.readLine();
  43.             column = line.length();
  44.             int lastIndexWN = 0;
  45.             while (!line.equals("")) {
  46.                 row++;
  47.                 data.add(line);
  48.                 dataBufferWE.append(line + "#");
  49.                 int indexNS = row, incrementNS = row + 1;
  50.                 int indexNE = 0, incrementNE = 1;
  51.                 lastIndexWN += row > column ? column : row;
  52.                 int indexWN = lastIndexWN, incrementWN = row >= column ? column : row + 1;
  53.                 int position = 0;
  54.                 for(char letter : line.toCharArray()) {
  55.                     positionWE.add(row + "::" + position);
  56.                     dataBufferNS.insert(indexNS, letter);
  57.                     positionNS.add(indexNS, row + "::" + position);
  58.                     indexNS += incrementNS;
  59.                     dataBufferNE.insert(indexNE, letter);
  60.                     positionNE.add(indexNE, row + "::" + position);
  61.                     incrementNE = incrementNE < (row + 1) ? incrementNE + 1: incrementNE;
  62.                     indexNE += incrementNE;
  63.                     dataBufferWN.insert(indexWN, letter);
  64.                     positionWN.add(indexWN, row + "::" + position);
  65.                     indexWN += incrementWN;
  66.                     incrementWN -= (column - position) <= (row + 1) ? 1 : 0;
  67.                     position++;
  68.                 }
  69.                 positionWE.add("#");
  70.                 line = reader.readLine();
  71.             }
  72.             row++;
  73.             int indexNS = row;
  74.             while(indexNS < dataBufferNS.length() - 1) {
  75.                 dataBufferNS.insert(indexNS, "#");
  76.                 positionNS.add(indexNS, "#");
  77.                 indexNS += row + 1;
  78.             }
  79.             dataBufferNS.insert(0, "#");
  80.             positionNS.add(0, "#");
  81.             dataBufferNS.append("#");
  82.             positionNS.add("#");
  83.             int loopValue = row < column ? row : column;
  84.             int forwardIndex = 0;
  85.             int reverseIndex = dataBufferNE.length();
  86.             for(int index = 1; index < loopValue; index++) {
  87.                 reverseIndex -= index;
  88.                 forwardIndex += index;
  89.                 dataBufferNE.insert(reverseIndex, "#");
  90.                 positionNE.add(reverseIndex, "#");
  91.                 dataBufferNE.insert(forwardIndex, "#");
  92.                 positionNE.add(forwardIndex, "#");
  93.  
  94.                 dataBufferWN.insert(reverseIndex, "#");
  95.                 positionWN.add(reverseIndex, "#");
  96.                 dataBufferWN.insert(forwardIndex, "#");
  97.                 positionWN.add(forwardIndex, "#");
  98.                 reverseIndex++;
  99.                 forwardIndex++;
  100.             }
  101.             forwardIndex += loopValue;
  102.             while(forwardIndex < reverseIndex) {
  103.                 dataBufferNE.insert(forwardIndex, "#");
  104.                 positionNE.add(forwardIndex, "#");
  105.                 dataBufferWN.insert(forwardIndex, "#");
  106.                 positionWN.add(forwardIndex, "#");
  107.                 forwardIndex += loopValue;
  108.                 forwardIndex++;
  109.             }
  110.             dataBufferAll.append(dataBufferWE).append(dataBufferNE).append(dataBufferNS).append(dataBufferWN);
  111.             positionAll.addAll(positionWE);
  112.             positionAll.addAll(positionNE);
  113.             positionAll.addAll(positionNS);
  114.             positionAll.addAll(positionWN);
  115.              
  116.             line = reader.readLine();
  117.             while (line != null) {
  118.                 words.add(line);
  119.                 int length = line.length();
  120.                 int index = dataBufferAll.indexOf(line);
  121.                 String reverse = new StringBuffer(line).reverse().toString();
  122.                 line = reader.readLine();
  123.                 if(index >= 0) {
  124.                     usedCoordinates.addAll(positionAll.subList(index, index + length));
  125.                     continue;
  126.                 }
  127.                 index = dataBufferAll.indexOf(reverse);
  128.                 if(index >= 0) {
  129.                     usedCoordinates.addAll(positionAll.subList(index, index + length));
  130.                 }
  131.             }
  132.         } catch (IOException e) {
  133.             e.printStackTrace();
  134.         }
  135.         usedCoordinates.add("#");
  136.         String resultString = "";
  137.         int position = 0;
  138.         for(String value : positionWE) {
  139.             if(!usedCoordinates.contains(value)) {
  140.                 resultString += dataBufferWE.charAt(position);
  141.             }
  142.             position++;
  143.         }
  144.         return resultString;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement