您的位置:首页 > 运维架构

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

Class:PaternityTest
Method:possibleFathers
Parameters:String, String, String[]
Returns:int[]
Method signature:int[] possibleFathers(String child, String mother, String[] men)
(be sure your method is public)

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)
"ABCD"

"AXCY"

{ "SBTD", "QRCD" }

Returns: { 0 }

The example above.
1)
"ABCD"

"ABCX"

{ "ABCY", "ASTD", "QBCD" }

Returns: { 1,  2 }

"ABCY" could not be the father. "ASTD" could be the father, with the 'A' and 'D' coming from the father and the 'B' and 'C' coming from the mother. "QBCD" could also be the father, with either the 'B' and 'D' or the 'C' and 'D' coming from the
father.
2)
"ABABAB"

"ABABAB"

{ "ABABAB", "ABABCC", "ABCCDD", "CCDDEE" }

Returns: { 0,  1 }

3)
"YZGLSYQT"

"YUQRWYQT"

{"YZQLDPWT", "BZELSWQM", "OZGPSFKT", "GZTKFYQT", "WQJLSMQT"}

Returns: { }

4)
"WXETPYCHUWSQEMKKYNVP"

"AXQTUQVAUOSQEEKCYNVP"

{ "WNELPYCHXWXPCMNKDDXD",
"WFEEPYCHFWDNPMKKALIW",
"WSEFPYCHEWEFGMPKIQCK",
"WAEXPYCHAWEQXMSKYARN",
"WKEXPYCHYWLLFMGKKFBB" }

Returns: { 1,  3 }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

这首题居然是一次做对 ,调试也没有调。

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: