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

TCHS-4-500

阅读更多

Problem Statement

    

In music, each note has a relative duration. For example, a whole note lasts four times longer than a quarter note. Half notes, eighth notes, and sixteenth notes have one half, one eighth, and one sixteenth the duration of whole notes, respectively.

A musical composition is characterized by its time signature, which defines the number of beats per measure. Time signatures are written as fractions, where the numerator is the number of beats, and the denominator is the type of note represented by each beat. For example, a time signature of 2/4 indicates that each measure contains two beats, each of which are quarter notes. A time signature of 3/8 indicates that each measure contains three beats, each of which are eighth notes. For the purposes of this problem, we will only consider the following time signatures: 3/8, 2/4, 3/4 and 4/4.

It is difficult for an untrained musician to determine the time signature of a composition. It is even more difficult for a computer. You will be given a String duration containing the description of a composition. Each character of duration corresponds to a single note in the composition. 'W' denotes a whole note, 'H' a half note, 'Q' a quarter note, 'E' an eighth note, and 'S' a sixteenth note.

Determine the time signature of the composition using the following heuristic algorithm (which is not very good, but quite simple):

  1. Consider all the available time signatures (3/8, 2/4, 3/4 and 4/4). Keep only the time signatures for which the composition would contain an integral number of measures, and discard the rest.
  2. For each of the remaining time signatures, determine the number of notes in the composition that start and end in different measures. Keep only the time signatures for which this number is minimal.
  3. Select the smallest of the remaining time signatures (where 3/8 < 2/4 < 3/4 < 4/4).

Return the time signature as a String ("3/8", "2/4", "3/4", or "4/4"), or return "?/?" (quotes for clarity only) if there are no time signatures left after step 1.

Definition

    
Class: CompositionTimeSignature
Method: getTimeSignature
Parameters: String
Returns: String
Method signature: String getTimeSignature(String duration)
(be sure your method is public)
    
 

Constraints

- duration will have between 1 and 50 characters, inclusive.
- Each character in duration will be 'W', 'H', 'Q', 'E' or 'S'.

Examples

0)  
    
"QQEEQEEEQEQ"

Returns: "4/4"

The total duration of the composition (in whole notes) is:

1/4+1/4+1/8+1/8+1/4+1/8+1/8+1/8+1/4+1/8+1/4 = 2.

The number of measures in the composition for different time signatures is:

  • for time signature 3/8: 2/(3/8) = 5 1/3 measures;
  • for time signature 2/4: 2/(2/4) = 4 measures;
  • for time signature 3/4: 2/(3/4) = 2 2/3 measures;
  • for time signature 4/4: 2/(4/4) = 2 measures.

So only time signatures 2/4 and 4/4 will be left after the first step of the algorithm. For time signature 2/4 the composition looks like this ('|' characters denote measure dividers, and 'Q' and 'E' characters show when each note starts):"

Q Q E E Q E E E Q E Q | | | | | | | | | | | | | | |

For time signature 4/4 the composition looks like this:

Q Q E E Q E E E Q E Q | | | | | | | | |

So the number of notes that start and end in different measures is 1 for time signature 2/4 and 0 for time signature 4/4. Therefore only time signature 4/4 is left after the second step of the algorithm.

1)  
    
"S"

Returns: "?/?"

The composition is very short and will occupy less than one measure for any time signature.
2)  
    
"EEEEEEEEEEEEEEEEEEEEEEEE"

Returns: "3/8"

The first two steps of the algorithm will not throw away any time signatures. So the smallest possible time signature 3/8 will be chosen on the third step.
3)  
    
"QQQQQQQQQQQQ"

Returns: "2/4"

Here 3/8 and 3/4 will be thrown away after the second step of the algorithm and the smallest time signature among 2/4 and 4/4 will be chosen on the third step.
4)  
    
"EQHQQWEEHSEEQWQEHHEEQSQEQHESQSWQESQEWWSSHQWQHQWSQW"

Returns: "3/4"

 

 

public class CompositionTimeSignature {

	public String getTimeSignature(String d) {
		int[] mods = {6, 8, 12, 16};
		String[] sigs = {"3/8", "2/4", "3/4", "4/4"};
		String t = "SEQHW", ret = "?/?";
		int len = 0, min = Integer.MAX_VALUE;
		for (int i = 0; i < d.length(); i++)
			len += 1 << (t.indexOf(d.charAt(i)));
		for (int i = 0; i < mods.length; i++) {
			if (len % mods[i] != 0)
				continue;
			int ruler = 0, cnt = 0;
			for (int j = 0; j < d.length(); j++) {
				ruler += 1 << t.indexOf(d.charAt(j));
				if (ruler > mods[i])
					cnt++;
				ruler %= mods[i];
			}
			if (cnt < min) {
				min = cnt;
				ret = sigs[i];
			}
		}
		return ret;
	}
	
}

 

分享到:
评论

相关推荐

    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`函数输出计算结果,包括基站数量、链路接...

    基站代维考试复习题汇总.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...

    Bit Error Rate Testing with CMU300

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

    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