package Something;
import java.io.*;
import java.util.*;
/*
Освобождаем ресурсы
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String path = reader.readLine();
//String path = "/Users/user/Desktop/lessons/file.txt";
ArrayList<String> words = new ArrayList<>();
try (BufferedReader file = new BufferedReader(new FileReader(path))) {
words.addAll(Arrays.asList(file.readLine().split(" ")));
}
String[] array = new String[words.size()];
for (int i = 0; i < words.size(); i++) {
array[i] = words.get(i);
}
StringBuilder result = getLine(array);
System.out.println(result);
}
public static StringBuilder getLine(String... words) {
String[] arrayMain = words;
String[] lowerCaseArray = new String[arrayMain.length];
for (int i = 0; i < arrayMain.length; i++) {
lowerCaseArray[i] = arrayMain[i].toLowerCase(Locale.ROOT);
}
LinkedList<String> list = new LinkedList<>();
boolean AB = false;
// while (!AB){
for (int i = 0; i < lowerCaseArray.length; i++) {
for (int j = 0; j < lowerCaseArray.length; j++) {
char jChar = lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1);
char iChar = lowerCaseArray[i].charAt(lowerCaseArray[i].length() - 1);
if (i != j && iChar == lowerCaseArray[j].charAt(0)) { // bee elephant
if (list.size() == 0) { // bee elephant
list.add(lowerCaseArray[i]);
list.add(lowerCaseArray[j]);
lowerCaseArray[i] = "AB";
lowerCaseArray[j] = "AB";
// i = lowerCaseArray.length-1;
}
int count = 0;
for (int k = 1; k < list.size(); k++) {
if (jChar == list.get(k).charAt(0) && lowerCaseArray[j].charAt(0) == list.get(k - 1).charAt(list.get(k - 1).length() - 1) && !lowerCaseArray[j].equals("AB")) {
list.add(k, lowerCaseArray[j]);
lowerCaseArray[j] = "AB";
count++;
break;
} else if (lowerCaseArray[j].charAt(0) == list.get(k).charAt(list.get(k).length() - 1) && /*lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1) == list.get(k + 1).charAt(0) &&*/ !lowerCaseArray[j].equals("AB")) {
try {
if ((lowerCaseArray[i].charAt(lowerCaseArray[i].length() - 1) == list.get(k + 1).charAt(0))) {
// if ((lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1) == list.get(k + 1).charAt(0)))
// {
list.add(lowerCaseArray[j]);
count++;
lowerCaseArray[j] = "AB";
//}
}
} catch (IndexOutOfBoundsException e) {
//throw new IndexOutOfBoundsException ("this is my exception for the condition");
list.add(lowerCaseArray[j]);
count++;
lowerCaseArray[j] = "AB";
}
}
if (iChar == list.get(k).charAt(0) && lowerCaseArray[i].charAt(0) == list.get(k - 1).charAt(list.get(k - 1).length() - 1) && !lowerCaseArray[i].equals("AB")) {
list.add(k, lowerCaseArray[i]);
lowerCaseArray[i] = "AB";
count++;
break;
} else if (lowerCaseArray[i].charAt(0) == list.get(k).charAt(list.get(k).length() - 1) && /*((lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1) == list.get(k + 1).charAt(0)) ||) && */!lowerCaseArray[i].equals("AB")) {
// if (list.size()> k) {
try {
if ((lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1) == list.get(k + 1).charAt(0))) {
// if ((lowerCaseArray[j].charAt(lowerCaseArray[j].length() - 1) == list.get(k + 1).charAt(0)))
// {
list.add(lowerCaseArray[i]);
count++;
lowerCaseArray[i] = "AB";
//}
}
} catch (IndexOutOfBoundsException e) {
//throw new IndexOutOfBoundsException ("this is my exception for the condition");
list.add(lowerCaseArray[i]);
count++;
lowerCaseArray[i] = "AB";
}
}
}
if (count > 0) {
if (jChar == list.get(0).charAt(0) && !lowerCaseArray[j].equals("AB")) {
list.add(0, lowerCaseArray[j]);
lowerCaseArray[j] = "AB";
}
if (iChar == list.get(0).charAt(0) && !lowerCaseArray[i].equals("AB")) {
list.add(0, lowerCaseArray[i]);
lowerCaseArray[i] = "AB";
}
if (lowerCaseArray[j].charAt(0) == list.get(list.size() - 1).charAt(list.get(list.size() - 1).length() - 1) && !lowerCaseArray[j].equals("AB")) {
list.add(lowerCaseArray[j]);
lowerCaseArray[j] = "AB";
}
if (lowerCaseArray[i].charAt(0) == list.get(list.size() - 1).charAt(list.get(list.size() - 1).length() - 1) && !lowerCaseArray[i].equals("AB")) {
list.add(lowerCaseArray[i]);
lowerCaseArray[i] = "AB";
}
}
} else {
int count = 0;
if (list.size() > 0) {
for (int k = 1; k < list.size(); k++) {
if (i != j && jChar == list.get(k).charAt(0) && lowerCaseArray[j].charAt(0) == list.get(k - 1).charAt(list.get(k - 1).length() - 1) && !lowerCaseArray[j].equals("AB")) {
list.add(k, lowerCaseArray[j]);
lowerCaseArray[j] = "AB";
count++;
} else {
count++;
}
if (i != j && iChar == list.get(k).charAt(0) && lowerCaseArray[i].charAt(0) == list.get(k - 1).charAt(list.get(k - 1).length() - 1) && !lowerCaseArray[i].equals("AB")) {
list.add(k, lowerCaseArray[i]);
lowerCaseArray[i] = "AB";
count++;
} else {
count++;
}
}
if (count > 0) {
if (i != j && jChar == list.get(0).charAt(0) && !lowerCaseArray[j].equals("AB")) {
list.add(0, lowerCaseArray[j]);
lowerCaseArray[j] = "AB";
}
if (i != j && iChar == list.get(0).charAt(0) && !lowerCaseArray[i].equals("AB")) {
list.add(0, lowerCaseArray[i]);
lowerCaseArray[i] = "AB";
}
}
}
}
}
}
for (String s: lowerCaseArray){
if (!s.equals("AB")) {
if (s.charAt(s.length()-1) == list.get(0).charAt(0)){
list.add(0,s);
}
else list.add(s);
}
}
//
// boolean result = true;
// for (String s: lowerCaseArray){
// if (!s.equals("AB")) result = false ;
// if (list.size() == 0) result = false ;
// }
// AB = result;
//}
LinkedHashMap<Integer, Integer> nums = new LinkedHashMap<>();
for (int i = 0; i < arrayMain.length; i++) {
for (int j = 0; j < list.size(); j++) {
if (arrayMain[i].equalsIgnoreCase(list.get(j))) {
nums.put(j, i);
}
}
}
// for (String l : lowerCaseArray) {
// System.out.print(l + " ");
// }
String resultS = "";
for (int i = 0; i < list.size(); i++) {
resultS += arrayMain[nums.get(i)] + " ";
}
StringBuilder sb = new StringBuilder(resultS.trim());
return sb;
}
}package com.javarush.task.task22.task2209;
import java.io.*;
import java.util.*;
/*
Освобождаем ресурсы
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String path = reader.readLine();
//String path = "/Users/user/Desktop/lessons/file.txt";
ArrayList<String> words = new ArrayList<>();
LinkedList<String> sList = new LinkedList<>();
try (BufferedReader file = new BufferedReader(new FileReader(path))) {
words.addAll(Arrays.asList(file.readLine().split(" ")));
}
String[] array = new String[words.size()];
for (int i = 0; i < words.size(); i++) {
array[i] = Character.toLowerCase(words.get(i).charAt(0)) + words.get(i).substring(1);
}
StringBuilder result = getLine(array);
System.out.println(result);
}
public static StringBuilder getLine(String... words) {
String[] array = words;
String[] arrayA = new String[array.length];
for (int i = 0; i < array.length; i++) {
arrayA[i] = Character.toUpperCase(array[i].charAt(0)) + array[i].substring(1);
}
LinkedList<String> sortedList = new LinkedList<>();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i].charAt(array[i].length() - 1) == array[j].charAt(0)) {
String iS = Character.toUpperCase(array[i].charAt(0)) + array[i].substring(1);
String jS = Character.toUpperCase(array[j].charAt(0)) + array[j].substring(1);
if (sortedList.size() == 0) {
sortedList.add(iS);
sortedList.add(jS);
} else if (Character.toUpperCase(array[j].charAt(((array[j].length() - 1)))) == sortedList.get(0).charAt(0)) {
sortedList.add(0, jS);
sortedList.add(0, iS);
}
}
}
}
for (int i = 0; i < arrayA.length; i++) {
if (!sortedList.contains(arrayA[i])) {
for (int j = 0; j < sortedList.size(); j++) {
if (sortedList.get(j).charAt(sortedList.get(j).length() - 1) == (array[i].charAt(0))) {
sortedList.add(Character.toUpperCase(array[i].charAt(0)) + array[i].substring(1));
}
}
}
}
LinkedList<String> copy = new LinkedList<String>();
for (String o : sortedList) {
if (!copy.contains(o)) {
copy.add(o);
}
}
String result = "";
for (String l : copy) {
result += l + " ";
}
StringBuilder sb = new StringBuilder(result.trim());
return sb;
}
}