Advertisement
MrMarvel

DNA

Dec 16th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package s_est_la_examinie;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintWriter;
  10. import java.io.ObjectInputStream.GetField;
  11. import java.util.Date;
  12. import java.util.HashMap;
  13. import java.util.HashSet;
  14. import java.util.Iterator;
  15. import java.util.Map;
  16. import java.util.Set;
  17.  
  18. public class C {
  19.     private static final File fileIn = new File("input.txt");
  20.     private static final File fileOut = new File("output.txt");
  21.     private static HashMap<Key, HashSet<String>> map = new HashMap<Key, HashSet<String>>();
  22.    
  23.     public static void main(String[] args) throws IOException {
  24.         long time = new Date().getTime();
  25.         //
  26.         run(args);
  27.         //
  28.         System.out.println("----------------------------");
  29.         System.out.println(new Date().getTime() - time + " ms");
  30.     }
  31.    
  32.     static void run(String[] args) throws IOException {
  33.  
  34.         System.out.println(fileIn.getAbsolutePath());
  35.         BufferedReader in = new BufferedReader(new FileReader(fileIn));
  36.         PrintWriter out = new PrintWriter(new FileWriter(fileOut));
  37.        
  38.         byte N = Byte.parseByte(in.readLine());
  39.         String msg;
  40.         int A,T,C,G;
  41.         char a;
  42.         for (int i = 0; i < N; i++) {
  43.             msg = in.readLine();
  44.             A = (int) msg.chars().filter(ch -> ch == 'A').count();
  45.             T = (int) msg.chars().filter(ch -> ch == 'T').count();
  46.             C = (int) msg.chars().filter(ch -> ch == 'C').count();
  47.             G = (int) msg.chars().filter(ch -> ch == 'G').count();
  48.             Key key = new Key(A, T, C, G);
  49.             HashSet<String> list =  map.get(key);
  50.             if (list == null) {
  51.                 list = new HashSet<String>();
  52.                 map.put(key, list);
  53.             }
  54.             list.add(msg);
  55.            
  56.         }
  57.         in.close();
  58.         out.println(map.size());
  59.         for (HashSet<String> list : map.values()) {
  60.             Iterator<String> iter = list.iterator();
  61.             while (iter.hasNext()) {
  62.                 out.print(iter.next());
  63.                 if (iter.hasNext()) out.print(" ");
  64.             }
  65.             out.println();
  66.         }
  67.         out.close();
  68.        
  69.     }
  70. }
  71.  
  72. class Key {
  73.     private int A, T, C, G;
  74.    
  75.     public Key(int A, int T, int C, int G) {
  76.         this.A = A;
  77.         this.T = T;
  78.         this.C = C;
  79.         this.G = G;
  80.     }
  81.  
  82.     @Override
  83.     public int hashCode() {
  84.         final int prime = 31;
  85.         int result = 1;
  86.         result = prime * result + A;
  87.         result = prime * result + C;
  88.         result = prime * result + G;
  89.         result = prime * result + T;
  90.         return result;
  91.     }
  92.  
  93.     @Override
  94.     public boolean equals(Object obj) {
  95.         if (this == obj)
  96.             return true;
  97.         if (obj == null)
  98.             return false;
  99.         if (getClass() != obj.getClass())
  100.             return false;
  101.         Key other = (Key) obj;
  102.         if (A != other.A)
  103.             return false;
  104.         if (C != other.C)
  105.             return false;
  106.         if (G != other.G)
  107.             return false;
  108.         if (T != other.T)
  109.             return false;
  110.         return true;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement