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

TCHS-2-1000

阅读更多

Problem Statement

     A spell is defined by its incantation, the word spoken when the spell is being cast. If you know the incantation of a spell, you can create counterspells in the following manner. First, you can optionally delete any number of characters from the original incantation. Note that empty incantations are not valid so you cannot delete all characters. Then, you can replace any number of the remaining characters according to a set of replacement rules. The replacement rules tell you which letters can be replaced, and the letters with which they can be replaced. For example, if the original incantation is "AA" and the allowed replacement is 'A' to 'Z', the following counterspells are possible: "AA", "A", "Z", "ZZ", "AZ", "ZA".

The best counterspell is the one with the greatest power. The power of a spell is the product of all its character values modulo 77077, where the value for a character is its position in the alphabet (character 'A' has value 1, character 'B' value 2, character 'C' value 3, and so on). The best possible counterspell for "AA" in the example above is "ZZ", which has a power of 676.

Please note that if the allowed replacements are 'A' to 'B' and 'B' to 'C', the counterspell for "AB" is "BC" not "CC". You cannot change a character that has already been changed, even if it would lead to a more powerful spell.

You will be given a String spell, the incantation of the spell you are going to counter, and a String rules, the allowed replacements for letters. The first character in rules is the allowed replacement for the letter 'A', the second for 'B' and so on. The character '-' is used to denote a letter that cannot be replaced. Your program must return a String, the most powerful counterspell available. If multiple return values are possible, return the shortest among them. If a tie still exists return the lexicographically earliest.

Definition

    
Class: Wizarding
Method: counterspell
Parameters: String, String
Returns: String
Method signature: String counterspell(String spell, String rules)
(be sure your method is public)
    
 

Constraints

- spell will contain between 1 and 13 characters, inclusive.
- spell will contain only uppercase letters ('A'-'Z').
- rules will contain exactly 26 characters.
- rules will contain only uppercase letters ('A'-'Z') and dashes ('-').

Examples

0)  
    
"AA"

"Z-------------------------"

Returns: "ZZ"

The example from the problem statement.
1)  
    
"AB"

"ZS------------------------"

Returns: "ZS"

The possible counterspells are "AB", "A", "B", "Z", "S", "ZB", "AS" and "ZS".
"ZS" is the most powerful.
2)  
    
"ZZZZ"

"-------------------------Z"

Returns: "ZZZZ"

 
3)  
    
"ABCDE"

"ZYXXYXZZXYXXZZXZYYXZZZX---"

Returns: "ZXXE"

 
4)  
    
"ABCDEABCDEABC"

"ACBDESKADSLOEDDDASDBADEDAE"

Returns: "CCDECCECC"

 

public class Wizarding {

	String spell, rules;
	StringBuilder smax, sb = new StringBuilder();
	int max = -1;

	private int calc(StringBuilder sb) {
		long product = 1;
		for (int i = 0; i < sb.length(); i++)
			product *= (sb.charAt(i) - 'A' + 1);
		return (int) (product % 77077);
	}

	private void choose(int start, int step, int length) {
		if (step == length) {
			int now = calc(sb);
			if (now > max || now == max && sb.length() < smax.length()
					|| now == max && sb.length() == smax.length()
					&& sb.toString().compareTo(smax.toString()) < 0) {
				max = now;
				smax = new StringBuilder(sb);
			}
			return;
		}
		for (int i = start; i < spell.length(); i++) {
			sb.append(spell.charAt(i));
			choose(i + 1, step + 1, length);
			sb.deleteCharAt(sb.length() - 1);
			if (rules.charAt(spell.charAt(i)-'A') == '-')
				continue;
			sb.append(rules.charAt(spell.charAt(i)-'A'));
			choose(i + 1, step + 1, length);
			sb.deleteCharAt(sb.length() - 1);
		}
	}

	public String counterspell(String spell, String rules) {
		this.spell = spell;
		this.rules = rules;
		for (int i = 1; i <= spell.length(); i++)
			choose(0, 0, i);
		return smax.toString();
	}
	
}

 

分享到:
评论

相关推荐

    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 寂寞...

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

    2. **C语言编程实践** - 文件包含了C语言代码示例 - 涉及到的基本输入输出操作 - 数学函数的应用 - 循环结构与条件判断 - 数据类型与变量定义 ### 3G移动通信技术详解 #### WCDMA (宽带码分多址) - **技术...

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