Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.sql.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class words {
- private static ArrayList<String> wordsIn = new ArrayList<>();
- private static ArrayList<String> wordsOut = new ArrayList<>();
- private static ArrayList<String> currentArray = new ArrayList<>();
- private static int currentCount;
- private static int countIn;
- public static void main(String[] args) throws IOException{
- fileRead("listOfWords");
- System.out.println(countIn);
- System.out.println(wordsIn);
- for (int i = 0; i < wordsIn.size(); i++){
- currentArray.add(wordsIn.get(0));
- backtrack(1);
- }
- fileWrite("outFile");
- }
- public static boolean existA(String a) throws IOException{
- for (String s : currentArray) {
- if (a.equals(s)) {
- return true;
- }
- }
- return false;
- }
- public static void backtrack(int k) throws IOException{
- for (int i = 0; i<wordsIn.size(); i++){
- String rightWord = wordsIn.get(i);
- String leftWord = currentArray.get(k-1);
- if ((rightWord.charAt(0)==leftWord.charAt(leftWord.length()-1))&&(existA(rightWord))){
- currentArray.add(rightWord);
- if(k>currentCount){
- i = 0;
- while (i<k+1){
- wordsOut.add(currentArray.get(i));
- System.out.println(wordsOut.get(i));
- i++;
- }
- currentCount = k;
- }
- backtrack(k + 1);
- currentArray.clear();
- }
- }
- }
- public static void fileRead(String fileName) throws IOException{
- List<String> list = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
- countIn = Integer.parseInt(list.get(0));
- for (int i = 1; i < list.size(); i++) {
- wordsIn.add(list.get(i));
- }
- }
- public static void fileWrite(String fileName) throws IOException{
- FileWriter writer = new FileWriter(fileName, false);
- writer.write(wordsOut.size() + "\n");
- System.out.println("______________");
- for (String s: wordsOut){
- System.out.println(s);
- writer.write(s + "\n");
- }
- writer.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement