package preparazione_esame; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class Esame2 { public static void main(String[] args) { HashMap> mappa = new HashMap<>(); try (BufferedReader br = new BufferedReader(new FileReader("src/preparazione_esame/esame2.txt")); PrintWriter pw1 = new PrintWriter(new FileWriter("esame2-1.txt")); PrintWriter pw2 = new PrintWriter(new FileWriter("esame2-2.txt")); PrintWriter pw3 = new PrintWriter(new FileWriter("esame2-3.txt")); PrintWriter pw4 = new PrintWriter(new FileWriter("esame2-4.txt")); PrintWriter pw5a = new PrintWriter(new FileWriter("esame2-5a.txt")); PrintWriter pw5b = new PrintWriter(new FileWriter("esame2-5b.txt"))) { /* * Acquisizione input */ String line = br.readLine(); String[] dati; String[] parole; ArrayList lista; // occhio a dove la ri-create while (line != null) { lista = new ArrayList<>(); // occhio a dove la ri-create dati = line.split(":"); // dati[0] = nome sequenza, dati[1] = stringa con lista parole parole = dati[1].split(","); for (String s : parole) { lista.add(s.trim()); } if (mappa.get(dati[0]) == null) { mappa.put(dati[0], lista); } else { mappa.get(dati[0]).addAll(lista); } System.out.println(mappa); line = br.readLine(); } /* * Elaborazione */ // punto 1 for (String key : mappa.keySet()) { if (mappa.get(key).size() % 2 != 0) { System.out.println("La sequenza " + key + " e' dispari"); pw1.println("La sequenza " + key + " e' dispari"); } } // punto 2 int maxSize = 0; String maxKey = ""; for (String key : mappa.keySet()) { if (mappa.get(key).size() > maxSize) { maxSize = mappa.get(key).size(); maxKey = key; } } System.out.println("Sequenza piu' lunga: " + maxKey); pw2.println("Sequenza piu' lunga: " + maxKey); // punto 3 boolean found = false; ArrayList done = new ArrayList<>(); for (String key : mappa.keySet()) { for (String parola : mappa.get(key)) { for (String anotherKey : mappa.keySet()) { if (!anotherKey.equals(key)) { for (String anotherParola : mappa.get(anotherKey)) { if (parola.equals(anotherParola) && !done.contains(parola)) { found = true; done.add(parola); System.out.println("La parola " + parola + " compare in " + key + " e " + anotherKey); pw3.println("La parola " + parola + " compare in " + key + " e " + anotherKey); } } } } } } if (!found) { System.out.println("Nessuna parola compare in piu' sequenze"); pw3.println("Nessuna parola compare in piu' sequenze"); } // punto 4 int[][] commons = new int[mappa.size()][mappa.size()]; int riga; int colonna; for (String key : mappa.keySet()) { for (String parola : mappa.get(key)) { for (String anotherKey : mappa.keySet()) { for (String anotherParola : mappa.get(anotherKey)) { if (parola.equals(anotherParola)) { riga = Integer.parseInt(key.substring(1, key.length())); colonna = Integer.parseInt(anotherKey.substring(1, anotherKey.length())); commons[riga - 1][colonna - 1] += 1; } } } } } System.out.println("Matrice parole in comune:"); pw4.println("Matrice parole in comune:"); for (int i = 0; i < commons.length; i++) { for (int j = 0; j < commons[0].length; j++) { System.out.printf("%02d ", commons[i][j]); pw4.printf("%02d ", commons[i][j]); } System.out.println(); pw4.println(); } // punto 5a (SUPPONETE di sapere come ordinare una lista, vedi MAGIA seguente) HashMap> pairs = new HashMap<>(); ArrayList coppie = null; for (int i = 1; i <= commons.length; i++) { for (int j = 1; j <= commons[0].length; j++) { if (i > j) { if (pairs.get(commons[i - 1][j - 1]) != null) { pairs.get(commons[i - 1][j - 1]).add("S" + i + "-S" + j); } else { coppie = new ArrayList<>(); coppie.add("S" + i + "-S" + j); pairs.put(commons[i - 1][j - 1], coppie); } } } } System.out.println("Coppie ordinate di sequenze con parole in comune:"); pw5a.println("Coppie ordinate di sequenze con parole in comune:"); ArrayList ordinate = new ArrayList<>(); ordinate.addAll(pairs.keySet()); /* * MAGIA: questi due metodi ve li avrei scritti nel testo del * compito */ Collections.sort(ordinate); Collections.reverse(ordinate); for (Integer c : ordinate) { System.out.println(pairs.get(c) + " : " + c); pw5a.println(pairs.get(c) + " : " + c); } // punto 5b (non vi fornisco i metodi sopra) int[] chiavi = new int[pairs.size()]; String[][] valori = new String[pairs.size()][]; int i = 0; int j; for (Integer key : pairs.keySet()) { chiavi[i] = key; j = 0; valori[i] = new String[pairs.get(key).size()]; for (String sequenze : pairs.get(key)) { valori[i][j] = sequenze; j++; } i++; } boolean swap = true; int tempC; String[] tempV; while (swap) { swap = false; for (i = 0; i < chiavi.length - 1; i++) { if (chiavi[i] < chiavi[i + 1]) { // scambio chiavi tempC = chiavi[i]; chiavi[i] = chiavi[i + 1]; chiavi[i + 1] = tempC; // scambio valori tempV = valori[i]; valori[i] = valori[i + 1]; valori[i + 1] = tempV; swap = true; } } } System.out.println("Coppie ordinate di sequenze con parole in comune:"); pw5b.println("Coppie ordinate di sequenze con parole in comune:"); for (int k = 0; k < chiavi.length; k++) { for (String v : valori[k]) { System.out.print(v + " "); pw5b.print(v + " "); } System.out.println(": " + chiavi[k]); pw5b.println(": " + chiavi[k]); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }