2012-01-10 5 views
1

문제는 다음과 같습니다.가능한 모든 행 조합 테스트

id value 
0: {1,2,3} 
0: {1,2,2} 

1: {1,2,3} 

2: {1,2,3} 
2: {1,1,3} 

나는 서로 간의 여러 행을 비교할 수있는 기능 equals이 : 비 고유 식별자가 여러 행이 있습니다. 나는 행을 함수 equals의 입력으로 선택하는 코드를 작성해야합니다. 선택한 행에는 고유 ID가 있어야하지만 고유 ID의 가능한 모든 조합을 확인해야합니다. 예를 들어, ids : 0,0,1,2,3의 행이 5 개있는 경우, 다음 두 가지 조합을 확인해야합니다. 0,1,2,3 및 0,1,2,3 두 번 apears. 물론이 두 조합은 모두 id = 0 인 고유 한 행으로 구성됩니다.

내 코드는 다음

public class Test { 
public static void main(String[] args) { 
    ArrayList<Row> allRows = new ArrayList<Row>(); 
    allRows.add(new Row(0,new int[]{1,2,3})); 
    allRows.add(new Row(0,new int[]{1,2,2})); 
    allRows.add(new Row(1,new int[]{1,2,3})); 
    allRows.add(new Row(2,new int[]{1,2,3})); 
    allRows.add(new Row(2,new int[]{1,1,3})); 

    boolean answer = hasEqualUniqueRows(allRows); 
} 

private boolean hasEqualUniqueRows(ArrayList<Row> allTokens) { 
    for (int i=0; i<allTokens.size(); i++) { 
     ArrayList<Integer[]> rows = new ArrayList<Integer[]>(); 
     rows = findUniqueRows(i,allTokens); 
     boolean answer = equalsExceptForNulls(rows); 
     if (answer) return true; 
    } 
    return false; 
} 
// Compare rows for similarities 
public static <T> boolean equalsExceptForNulls(ArrayList<T[]> ts) { 
    for (int i=0; i<ts.size(); i++) { 
     for (int j=0; j<ts.size(); j++) { 
      if (i != j) { 
       boolean answer = equals(ts.get(i),ts.get(j)); 
       if (!answer) return false; 
      } 
     } 
    } 
    return true; 
} 

public static <T> boolean equals(T[] ts1, T[] ts2) { 
    if (ts1.length != ts2.length) return false; 
    for(int i = 0; i < ts1.length; i++) { 
     T t1 = ts1[i], t2 = ts2[i]; 
     if (t1 != null && t2 != null && !t1.equals(t2)) 
      return false; 
    } 
    return true; 
} 

class Row { 
      private String key; 
      private Integer[] values; 

     public Row(String k,Integer[] v) { 
      this.key = k; 
      this.values = v; 
     } 

     public String getKey() { 
      return this.key; 
     } 

     public Integer[] getValues() { 
      return this.values; 
     } 
} 

}

고유 ID와 행의 수를 알 수 연역적이기 때문에, 나는하지는이 문제를 해결하는 방법을 알고있다. 어떤 제안? 감사.

편집 # 1 코드를 업데이트했습니다. 이제는 더 완벽 해졌습니다. 그러나 함수 findUniqueRows의 구현이 부족합니다. 이 함수는 고유 키 (ids)가있는 행을 ArrayList에서 선택해야합니다. 누군가이 기능을 개발하는 데 도움이 될 수 있습니까? 감사.

+0

* "어떤 제안이 있으십니까?"* 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. BTW -이 숙제는 무엇입니까? –

+0

아니, 숙제가 아니야. SSCCE를 게시 할 때의 문제점은 현재 'equals'함수의 현재 버전이 3 행을 입력으로 사용한다는 것입니다. 따라서 여기에 게시 할 수 없습니다. 나는 행의 조합을 단계별로 선택하는 코드 스 니펫이나 제안을 기다리고있다. 어쩌면 누군가가 개념적 해결책을 제시 할 수 있을까요? –

+0

고유 한 조합 만 생성하는 전략을 사용하면 중복을 찾아서 제거 할 필요가 없습니다. –

답변

1

중복이없는 모든 조합을 찾는 것이 목표라면 다음과 같이 할 수 있습니다. 중복을 찾는 테스트는 처음에 중복을 생성하지 않는다는 것을 확인하는 것입니다.

import java.util.*; 
import java.util.concurrent.atomic.AtomicInteger; 

public class Main { 
    public static void main(String... args) { 
     Bag<Integer> b = new Bag<>(); 
     b.countFor(1, 2); 
     b.countFor(2, 1); 
     b.countFor(3, 3); 
     Set<String> set = new LinkedHashSet<>(); 
     for (List<Integer> list : b.combinations()) { 
      System.out.println(list); 
      String s = list.toString(); 
      if (!set.add(s)) 
       System.err.println("Duplicate entry " + s); 
     } 
    } 
} 

class Bag<E> { 
    final Map<E, AtomicInteger> countMap = new LinkedHashMap<>(); 

    void countFor(E e, int n) { 
     countMap.put(e, new AtomicInteger(n)); 
    } 

    void decrement(E e) { 
     AtomicInteger ai = countMap.get(e); 
     if (ai.decrementAndGet() < 1) 
      countMap.remove(e); 
    } 

    void increment(E e) { 
     AtomicInteger ai = countMap.get(e); 
     if (ai == null) 
      countMap.put(e, new AtomicInteger(1)); 
     else 
      ai.incrementAndGet(); 
    } 

    List<List<E>> combinations() { 
     List<List<E>> ret = new ArrayList<>(); 
     List<E> current = new ArrayList<>(); 
     combinations0(ret, current); 
     return ret; 
    } 

    private void combinations0(List<List<E>> ret, List<E> current) { 
     if (countMap.isEmpty()) { 
      ret.add(new ArrayList<E>(current)); 
      return; 
     } 
     int position = current.size(); 
     current.add(null); 
     List<E> es = new ArrayList<>(countMap.keySet()); 
     if (es.get(0) instanceof Comparable) 
      Collections.sort((List) es); 
     for (E e : es) { 
      current.set(position, e); 
      decrement(e); 
      combinations0(ret, current); 
      increment(e); 
     } 
     current.remove(position); 
    } 
} 
관련 문제