Advertisement
Guest User

Untitled

a guest
Sep 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.sql.Array;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9.  
  10. public class words {
  11. private static ArrayList<String> wordsIn = new ArrayList<>();
  12. private static ArrayList<String> wordsOut = new ArrayList<>();
  13. private static ArrayList<String> currentArray = new ArrayList<>();
  14. private static int currentCount;
  15. private static int countIn;
  16. public static void main(String[] args) throws IOException{
  17. fileRead("listOfWords");
  18. System.out.println(countIn);
  19. System.out.println(wordsIn);
  20. for (int i = 0; i < wordsIn.size(); i++){
  21. currentArray.add(wordsIn.get(0));
  22. backtrack(1);
  23. }
  24. fileWrite("outFile");
  25. }
  26. public static boolean existA(String a) throws IOException{
  27. for (String s : currentArray) {
  28. if (a.equals(s)) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. public static void backtrack(int k) throws IOException{
  35. for (int i = 0; i<wordsIn.size(); i++){
  36. String rightWord = wordsIn.get(i);
  37. String leftWord = currentArray.get(k-1);
  38. if ((rightWord.charAt(0)==leftWord.charAt(leftWord.length()-1))&&(existA(rightWord))){
  39. currentArray.add(rightWord);
  40. if(k>currentCount){
  41. i = 0;
  42. while (i<k+1){
  43. wordsOut.add(currentArray.get(i));
  44. System.out.println(wordsOut.get(i));
  45. i++;
  46. }
  47. currentCount = k;
  48. }
  49. backtrack(k + 1);
  50. currentArray.clear();
  51. }
  52. }
  53. }
  54. public static void fileRead(String fileName) throws IOException{
  55. List<String> list = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
  56. countIn = Integer.parseInt(list.get(0));
  57. for (int i = 1; i < list.size(); i++) {
  58. wordsIn.add(list.get(i));
  59. }
  60. }
  61. public static void fileWrite(String fileName) throws IOException{
  62. FileWriter writer = new FileWriter(fileName, false);
  63. writer.write(wordsOut.size() + "\n");
  64. System.out.println("______________");
  65. for (String s: wordsOut){
  66. System.out.println(s);
  67. writer.write(s + "\n");
  68. }
  69. writer.close();
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement