Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pack;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class FindWords {
- private List<String> data = null, words = null;
- private StringBuffer dataBufferWE = null, dataBufferNS = null, dataBufferNE = null, dataBufferWN = null, dataBufferAll = null;
- private List<String> positionWE = null, positionNS = null, positionNE = null, positionWN = null, positionAll = null, usedCoordinates = null;
- private int row = -1, column = -1;
- public FindWords() {
- data = new ArrayList<String>();
- words = new ArrayList<String>();
- dataBufferWE = new StringBuffer();
- dataBufferNS = new StringBuffer();
- dataBufferNE = new StringBuffer();
- dataBufferWN = new StringBuffer();
- dataBufferAll = new StringBuffer();
- positionWE = new ArrayList<String>();
- positionNS = new ArrayList<String>();
- positionNE = new ArrayList<String>();
- positionWN = new ArrayList<String>();
- positionAll = new ArrayList<String>();
- usedCoordinates = new ArrayList<String>();
- }
- public static void main(String[] args) {
- FindWords findWords = new FindWords();
- String result = findWords.parseFile(args[0]);
- System.out.println(result);
- }
- public String parseFile(String fileName) {
- try {
- BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
- String line = reader.readLine();
- column = line.length();
- int lastIndexWN = 0;
- while (!line.equals("")) {
- row++;
- data.add(line);
- dataBufferWE.append(line + "#");
- int indexNS = row, incrementNS = row + 1;
- int indexNE = 0, incrementNE = 1;
- lastIndexWN += row > column ? column : row;
- int indexWN = lastIndexWN, incrementWN = row >= column ? column : row + 1;
- int position = 0;
- for(char letter : line.toCharArray()) {
- positionWE.add(row + "::" + position);
- dataBufferNS.insert(indexNS, letter);
- positionNS.add(indexNS, row + "::" + position);
- indexNS += incrementNS;
- dataBufferNE.insert(indexNE, letter);
- positionNE.add(indexNE, row + "::" + position);
- incrementNE = incrementNE < (row + 1) ? incrementNE + 1: incrementNE;
- indexNE += incrementNE;
- dataBufferWN.insert(indexWN, letter);
- positionWN.add(indexWN, row + "::" + position);
- indexWN += incrementWN;
- incrementWN -= (column - position) <= (row + 1) ? 1 : 0;
- position++;
- }
- positionWE.add("#");
- line = reader.readLine();
- }
- row++;
- int indexNS = row;
- while(indexNS < dataBufferNS.length() - 1) {
- dataBufferNS.insert(indexNS, "#");
- positionNS.add(indexNS, "#");
- indexNS += row + 1;
- }
- dataBufferNS.insert(0, "#");
- positionNS.add(0, "#");
- dataBufferNS.append("#");
- positionNS.add("#");
- int loopValue = row < column ? row : column;
- int forwardIndex = 0;
- int reverseIndex = dataBufferNE.length();
- for(int index = 1; index < loopValue; index++) {
- reverseIndex -= index;
- forwardIndex += index;
- dataBufferNE.insert(reverseIndex, "#");
- positionNE.add(reverseIndex, "#");
- dataBufferNE.insert(forwardIndex, "#");
- positionNE.add(forwardIndex, "#");
- dataBufferWN.insert(reverseIndex, "#");
- positionWN.add(reverseIndex, "#");
- dataBufferWN.insert(forwardIndex, "#");
- positionWN.add(forwardIndex, "#");
- reverseIndex++;
- forwardIndex++;
- }
- forwardIndex += loopValue;
- while(forwardIndex < reverseIndex) {
- dataBufferNE.insert(forwardIndex, "#");
- positionNE.add(forwardIndex, "#");
- dataBufferWN.insert(forwardIndex, "#");
- positionWN.add(forwardIndex, "#");
- forwardIndex += loopValue;
- forwardIndex++;
- }
- dataBufferAll.append(dataBufferWE).append(dataBufferNE).append(dataBufferNS).append(dataBufferWN);
- positionAll.addAll(positionWE);
- positionAll.addAll(positionNE);
- positionAll.addAll(positionNS);
- positionAll.addAll(positionWN);
- line = reader.readLine();
- while (line != null) {
- words.add(line);
- int length = line.length();
- int index = dataBufferAll.indexOf(line);
- String reverse = new StringBuffer(line).reverse().toString();
- line = reader.readLine();
- if(index >= 0) {
- usedCoordinates.addAll(positionAll.subList(index, index + length));
- continue;
- }
- index = dataBufferAll.indexOf(reverse);
- if(index >= 0) {
- usedCoordinates.addAll(positionAll.subList(index, index + length));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- usedCoordinates.add("#");
- String resultString = "";
- int position = 0;
- for(String value : positionWE) {
- if(!usedCoordinates.contains(value)) {
- resultString += dataBufferWE.charAt(position);
- }
- position++;
- }
- return resultString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement