Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- String pattern = "";
- List<String> liste = new ArrayList<>();
- String first = sc.nextLine();
- recursif(first.length(), first, liste);
- for(int i = 3; i <= n+1; i++) {
- String mot = sc.nextLine();
- List<String> inter = new ArrayList<>();
- recursif(mot.length(), mot, inter);
- List<String> nouveau = new ArrayList<>(liste);
- for(String chaine : liste) {
- boolean valide = false;
- for(String deuxieme : inter) {
- if(chaine.equals(deuxieme)) {
- valide = true;
- break;
- }
- }
- if(!valide) {
- nouveau.remove(chaine);
- }
- }
- liste = nouveau;
- }
- if(liste.size() == 0) {
- System.out.println("KO");
- return;
- }
- int z = 0;
- int maxSize = 0;
- /*List<String> nouveau = new ArrayList<>(liste);
- for(String s : liste) {
- if(s.length() > maxSize)
- maxSize = s.length();
- else
- nouveau.remove(s);
- }
- liste = nouveau;*/
- maxSize = 0;
- String fin = "";
- for(String s : liste) {
- if(s.length() > maxSize) {
- maxSize = s.length();
- fin = s;
- }
- }
- System.out.println(fin);
- }
- public static void recursif(int n, String s, List<String> liste) {
- List<String> inter = new ArrayList<>();
- for(int j = 0; j < s.length(); j++) {
- String chaine = s.substring(0, j)+s.substring(j+1, s.length());
- if(!chaine.equals("") && !liste.contains(chaine)) {
- liste.add(chaine);
- inter.add(chaine);
- }
- }
- for(String chaine : inter) {
- recursif(n-1, chaine, liste);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement