TopCoder 250 points 16-SRM 151 DIV 2 97.63/250 39.05%

Problem Statement

 A prefix code is a set of words in which no word is a prefix of another word in the set. A wordv is said to be a prefix of a word w if w starts with v.

An important property of prefix codes is that they are uniquely decodable. Prefix codes are commonly used - telephone numbers are an everyday example (as you probably don't want a stranger to pick up the phone call you make just because his number is a prefix of the number you intend to dial). Prefix codes are also very popular in computer science, the Huffman code used for data compression being a famous example.

Given a String[] words, return the String "Yes" if that set of words is a prefix code or return the String"No, i" if it is not, where i is replaced by the lowest 0-based index of a String inwords that is a prefix of another String in words. (That index should have no extra leading zeros.)

Definition

 
Class:PrefixCode
Method:isOne
Parameters:String[]
Returns:String
Method signature:String isOne(String[] words)
(be sure your method is public)
 
 

Notes

-Letters are case sensitive (e.g. "No" is not a prefix of "not").
-Do not forget the single space between the comma and i in "No, i"

Constraints

-words contains between 1 and 50 elements, inclusive.
-Each element of words contains between 1 and 50 characters, inclusive.
-Each element of words consists only of characters '0'-'9', 'A'-'Z' and 'a'-'z', inclusive.
-No two elements of words are equal (as the input represents a set).

Examples

0) 
 
{"trivial"}
Returns: "Yes"
As there is only one word, no word can be the prefix of another, so this is a trivial example of a prefix code.
1) 
 
{"10001", "011", "100", "001", "10"}
Returns: "No, 2"
"100" (at index 2) and "10" (at index 4) are both a prefix of "10001" and "10" is also a prefix of "100", therefore it is no prefix code. "100" is the prefix with the lowest index.
2) 
 
{"no", "nosy", "neighbors", "needed"}
Returns: "No, 0"
 
3) 
 
{"1010", "11", "100", "0", "1011"}
Returns: "Yes"
 
4) 
 
{"No", "not"}
Returns: "Yes"
 

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.     


public class PrefixCode {


	public  String isOne(String[] words) {
		int len = words.length;
		int min = 999;
		if (len == 1)
			return "Yes";

		for (int i = 0; i < len; i++) {
			int j = 0;
			for (; j < len; j++) {
				int n = -1;
				if (i == j)
					continue;
				if ((n = words[i].indexOf(words[j])) == 0) {
					if (j < min)
						min = j;
				}
			}

		}
		if (min == 999)
			return "Yes";
		else
			return "No, " + min;

	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值