TopCoder 300 points 23-SRM 155 DIV 1 116.71/300 38.90%
2013-06-14 01:12
726 查看
Problem Statement | ||||||||||||
DNA testing is one of the most popular methods of establishing paternity. In such a test, you compare samples of DNA from the man, the child, and the child's mother. For the purposes of this problem, assume that each sample is represented as a String of uppercase letters ('A'-'Z'). If half of the characters in the child's sample match the characters in the corresponding positions in the man's sample, and the remaining characters in the child's sample match the characters in the corresponding positions in the mother's sample, then the man is most likely the father. On the other hand, if it is impossible to partition the child's sample such that half of the characters match the man's and the other half match the mother's, then the man is definitely ruled out as the father. For example, suppose the child's sample is "ABCD" and the mother's sample is "AXCY" (all quotes for clarity only). The 'A' and 'C' in the child's sample must have come from the mother, so the 'B' and 'D' must have come from the father. If the man's sample is "SBTD", then he is most likely the father, but if the man's sample is "QRCD", then he is definitely not the father. Note in the latter case that the man was definitely ruled out even though half of his sample (the 'C' and 'D') did in fact match the child's. Your method will take samples from the child and the mother, as well as samples from several men, and return the indices of the men who cannot be ruled out as the father, in increasing order. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Notes | ||||||||||||
- | You may assume that the identity of the mother is not in question. | |||||||||||
Constraints | ||||||||||||
- | men contains between 1 and 5 elements, inclusive. | |||||||||||
- | child, mother, and all elements of men contain the same number of characters, which is even and between 2 and 20, inclusive. | |||||||||||
- | child, mother, and all elements of men contain only uppercase letters ('A'-'Z'). | |||||||||||
- | At least half of the characters in mother match the characters in the corresponding positions in child. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
这首题居然是一次做对 ,调试也没有调。
import java.util.ArrayList; public class PaternityTest { public int[] possibleFathers(String child, String mother, String[] men) { int len = mother.length(); int len_half = len / 2; int len_men = men.length; String man; ArrayList<Integer> notEqualList = new ArrayList<Integer>(); ArrayList<Integer> resultIndexList = new ArrayList<Integer>(); ArrayList<Integer> tmpList = new ArrayList<Integer>(); for (int i = 0; i < len; i++) tmpList.add(i); char m_array[] = mother.toCharArray(); char c_array[] = child.toCharArray(); char man_array[]; char mother_array[][] = new char[len][2]; for (int i = 0; i < len; i++) { mother_array[i][0] = m_array[i]; mother_array[i][1] = 'n'; if (m_array[i] == c_array[i]) { mother_array[i][1] = 'y'; } else { notEqualList.add(i); } } tmpList.removeAll(notEqualList); int len_notEqual = notEqualList.size(); int left = len_half - len_notEqual; for (int i = 0; i < len_men; i++) { boolean isFather = true; man = men[i]; man_array = man.toCharArray(); for (int j = 0; j < len_notEqual; j++) { int k = notEqualList.get(j); if (c_array[k] != man_array[k]) { isFather = false; break; } } if (!isFather) continue; int z = 0; for (int p = 0; p < len; p++) { if (z == left) break; if (notEqualList.contains(p)) continue; if (c_array[p] == man_array[p]) { z++; } } if (z == left) resultIndexList.add(i); } int num = resultIndexList.size(); if (num == 0) return new int[0]; int arr[] = new int[num]; for (int i = 0; i < num; i++) arr[i] = resultIndexList.get(i); return arr; } }
相关文章推荐
- TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%
- TopCoder 300 points 27-SRM 157 DIV 1 90/300 30%
- TopCoder 300 points 5-SRM 146 DIV 1 109.50/300 36.5%
- TopCoder 300 points 25-SRM 156 DIV 1 90.77/300 30.26%
- TopCoder 350 points 22-SRM 154 DIV 2 246.20/300 82.07%
- TopCoder 300 points 1-SRM 144 DIV 1 90/300 30%
- TopCoder 300 points 26-SRM 156 DIV 2 78.06/300 26.02%
- topcoder srm 692 div1 -23
- TopCoder 200 points 2-SRM 144 DIV 2 152.06/200 76.03%
- TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
- TopCoder 250 points 17-SRM 152 DIV 1 106.51/250 42.60%
- topcoder srm 300 div1
- TopCoder 250 points 3-SRM 145 DIV 1 84.71/250 33.88%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%
- TopCoder 250 points 19-SRM 153 DIV 1 84.72/250 33.89%
- TopCoder 250 points 20-SRM 153 DIV 2 216.58/250 86.63%
- TopCoder SRM 658 Div1 300 - OddEvenTree (树的性质 + 构造)
- TopCoder 250 points 6-SRM 146 DIV 2 162.67/250 65.07%
- TopCoder 550 points 1-SRM 144 DIV 1 165/550 30%