Guest User

Genome sequencing

a guest
Feb 5th, 2015
318
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. /**
  6.  * Auto-generated code below aims at helping you parse
  7.  * the standard input according to the problem statement.
  8.  **/
  9. class Solution {
  10.     List<List<String>> permutations = new ArrayList<List<String>>();
  11.  
  12.     public static void main(String args[]) {
  13.         List<String> subseqs = new ArrayList<String>();
  14.        
  15.         Scanner in = new Scanner(System.in);
  16.         int N = in.nextInt();
  17.         for (int i = 0; i < N; i++) {
  18.             String subseq = in.next();
  19.             System.err.println(subseq);
  20.             subseqs.add(subseq);
  21.         }
  22.         Collections.reverse(subseqs);
  23.        
  24.         PermutationAndSequencingHandler handler = new PermutationAndSequencingHandler(N, subseqs);
  25.         handler.permute();
  26.         int shortest = handler.findShortestSequence();
  27.         System.out.println(String.valueOf(shortest));
  28.     }
  29. }
  30.  
  31. class PermutationAndSequencingHandler {
  32.     int n;
  33.     private List<String> elements;
  34.     private List<String[]> permutations = new ArrayList<String[]>();
  35.    
  36.     public PermutationAndSequencingHandler (int n, List<String> elements) {
  37.         this.n = n;
  38.         this.elements = elements;
  39.     }
  40.    
  41.     public void permute() {
  42.         permute(0, new String[n]);
  43.     }
  44.    
  45.     private void permute(int index, String[] array) {
  46.         if (index == n) {
  47.             permutations.add(array);
  48.         } else {
  49.             String element = elements.get(index);
  50.             for (int i = 0; i < n; ++i) {
  51.                 if (array[i] == null) {
  52.                     String[] arr = Arrays.copyOf(array, n);
  53.                     arr[i] = element;
  54.                     permute(index + 1, arr);
  55.                 }
  56.             }
  57.            
  58.         }
  59.     }
  60.    
  61.     public int findShortestSequence() {
  62.         int shortest = Integer.MAX_VALUE;
  63.         for (String[] permutation : permutations) {
  64.             int length = calcLength(permutation);
  65.             if (length < shortest) {
  66.                 shortest = length;
  67.             }
  68.         }
  69.         return shortest;
  70.     }
  71.    
  72.     private int calcLength(String[] permutation) {
  73.         StringBuilder builder = new StringBuilder();
  74.         builder.append(permutation[0]);
  75.         for (int i = 1; i < n; ++i) {
  76.             int j = i - 1;
  77.            
  78.             String first = permutation[j];
  79.             String second = permutation[i];
  80.            
  81.             int bestLength = 0;
  82.             if (first.contains(second)) {
  83.                 bestLength = second.length();
  84.             } else {
  85.                 for (int ec = 1; ec < Math.min(first.length(), second.length()); ++ec) {
  86.                     String firstSub = first.substring(first.length() - (ec));
  87.                     String secondSub = second.substring(0, ec);
  88.                     if (firstSub.equals(secondSub)) {
  89.                         bestLength = ec;
  90.                     }
  91.                 }
  92.             }
  93.            
  94.             builder.append(second.substring(bestLength));
  95.         }
  96.         System.err.println(builder.toString());
  97.         return builder.toString().length();
  98.     }
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×