Advertisement
Guest User

Exo4 BattleDev

a guest
Mar 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         Scanner sc = new Scanner(System.in);
  3.        
  4.         int n = Integer.parseInt(sc.nextLine());
  5.        
  6.         String pattern = "";
  7.        
  8.         List<String> liste = new ArrayList<>();
  9.        
  10.         String first = sc.nextLine();
  11.        
  12.         recursif(first.length(), first, liste);
  13.        
  14.         for(int i = 3; i <= n+1; i++) {
  15.             String mot = sc.nextLine();
  16.            
  17.             List<String> inter = new ArrayList<>();
  18.            
  19.             recursif(mot.length(), mot, inter);
  20.            
  21.             List<String> nouveau = new ArrayList<>(liste);
  22.            
  23.             for(String chaine : liste) {
  24.                 boolean valide = false;
  25.                 for(String deuxieme : inter) {
  26.                     if(chaine.equals(deuxieme)) {
  27.                         valide = true;
  28.                         break;
  29.                     }
  30.                 }
  31.                
  32.                 if(!valide) {
  33.                     nouveau.remove(chaine);
  34.                 }
  35.             }
  36.            
  37.             liste = nouveau;
  38.         }
  39.        
  40.         if(liste.size() == 0) {
  41.             System.out.println("KO");
  42.             return;
  43.         }
  44.        
  45.         int z = 0;
  46.         int maxSize = 0;
  47.        
  48.         /*List<String> nouveau = new ArrayList<>(liste);
  49.         for(String s : liste) {
  50.             if(s.length() > maxSize)
  51.                 maxSize = s.length();
  52.             else
  53.                 nouveau.remove(s);
  54.         }
  55.        
  56.         liste = nouveau;*/
  57.        
  58.         maxSize = 0;
  59.         String fin = "";
  60.         for(String s : liste) {
  61.             if(s.length() > maxSize) {
  62.                 maxSize = s.length();
  63.                 fin = s;
  64.             }
  65.         }
  66.        
  67.        
  68.        
  69.         System.out.println(fin);
  70.        
  71.  
  72.     }
  73.    
  74.     public static void recursif(int n, String s, List<String> liste) {
  75.        
  76.         List<String> inter = new ArrayList<>();
  77.        
  78.         for(int j = 0; j < s.length(); j++) {
  79.             String chaine = s.substring(0, j)+s.substring(j+1, s.length());
  80.             if(!chaine.equals("") && !liste.contains(chaine)) {
  81.                 liste.add(chaine);
  82.                 inter.add(chaine);
  83.             }
  84.         }
  85.        
  86.         for(String chaine : inter) {
  87.             recursif(n-1, chaine, liste);
  88.         }
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement