TopCoder 300 points 23-SRM 155 DIV 1 116.71/300 38.90%

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;

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值