`
superhack
  • 浏览: 32075 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-11-1000

Go 
阅读更多

Problem Statement

    

You have beads of several different colors that are to be placed on a string, with the requirement that for any group of three adjacent beads, all three must have different colors.

You are given a int[] beads indicating how many of each color bead you have. The i-th element of beads is the number of beads of color i, where each i represents a distinct color. You are to return a long representing the number of ways they can be placed on the string, meeting the given requirement.

Definition

    
Class: StringBeads
Method: numWays
Parameters: int[]
Returns: long
Method signature: long numWays(int[] beads)
(be sure your method is public)
    
 

Notes

- The resulting arrangement of beads is linear, not circular. (Thus, the first bead on the string, and the last, are not adjacent.)

Constraints

- beads will contain between 3 and 5 elements, inclusive.
- Each element of beads will be between 1 and 10, inclusive.
- The total number of beads will not exceed 35.

Examples

0)  
    
{ 1, 1, 1 }
Returns: 6
There are three beads, each a different color. The number of ways to arrange them is simply 3! = 6.
1)  
    
{ 1, 1, 1, 1 }
Returns: 24
Now with four beads, there are 4! = 24 arrangements.
2)  
    
{ 2, 1, 1 }
Returns: 2
Lets say that we have 2 green beads, 1 blue, and 1 red. In order to satisfy the requirement that each group of three consecutive beads have no two of the same color, the green beads must go at the beginning and end of the string. There are two ways to put the blue and the red in the middle: GRBG or GBRG.
3)  
    
{ 3, 1, 1 }
Returns: 0
There is no way to meet the requirement.
4)  
    
{ 3, 2, 2 }
Returns: 2
 
import java.util.*;

public class StringBeads {

	Map<String, Long> memo = new HashMap<String, Long>();
	String end = "";

	public long numWays(int[] beads) {
		StringBuilder buf = new StringBuilder("##");
		for (int i : beads) {
			buf.append((char) (i + '0'));
			end += '0';
		}
		return go(buf.toString());
	}

	private long go(String s) {
		if (memo.containsKey(s))
			return memo.get(s);
		String rem = s.substring(2);
		if (rem.equals(end))
			return 1;
		long count = 0;
		int a = s.charAt(0) - '0';
		int b = s.charAt(1) - '0';
		char[] left = rem.toCharArray();
		for (int i = 0; i < left.length; i++)
			if (left[i] > '0' && i != a && i != b) {
				left[i]--;
				count += go((char) (b + '0') + "" + i + new String(left));
				left[i]++;
			}
		memo.put(s, count);
		return count;
	}

}

 

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    - 计算`PDCHS_zhuan`和`TCHS_zhuan`(专用数据信道和语音信道转换后的数量)。 - 计算`DSP_pdch`和`DSP_tch`(所需的数字信号处理器数量)。 #### 输出结果 - 使用`printf`函数输出计算结果,包括基站数量、链路接...

    Bit Error Rate Testing with CMU300

    电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...

    基站代维考试复习题汇总.doc

    TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 &lt;input type="text" tchs=""&gt;&lt;/input&gt; 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

    TOPCODER 算法PPT1

    此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...

Global site tag (gtag.js) - Google Analytics