Problem Statement
|
|
Three kids are playing the following game: A word is written on a sheet of paper, and one after another, the players take turns adding letters to the word. During each turn, a player must add a single letter to either the beginning or the end of the word. If a player cannot add a letter during his turn, the game ends and that player is declared the loser.
During the game, a player might cheat by making an illegal move. Any move where the player does not add exactly one letter to either the beginning or the end of the word is considered illegal. The only exception is the last move of the game, when the loser adds no letters to the word. Given the log of the game, you must determine whether any of the players cheated.
You are also given the String[]s first, second, and third, containing the chronological lists of words seen by each of the players at the beginning of their turns. Element 0 of first is therefore the original word, element 0 of second is the word after the first player makes his first move, element 0 of third is the word after the second player makes his first move, etc. Return 1, 2, or 3 if the first, second, or third player, respectively, cheated. If multiple players cheated, return the player who cheated first. If nobody cheated, return -1.
|
Definition
|
|
Class: |
KidsWordGame |
Method: |
getCheater |
Parameters: |
String[], String[], String[] |
Returns: |
int |
Method signature: |
int getCheater(String[] first, String[] second, String[] third) |
(be sure your method is public) |
|
|
|
Constraints
|
- |
first will contain between 1 and 50 elements, inclusive. |
- |
second and third each will contain between 0 and 50 elements, inclusive. |
- |
Each element of first, second and third will contain between 0 and 50 characters, inclusive. |
- |
Each element of first, second and third will contain only lowercase letters ('a'-'z'). |
- |
Define a = the number of elements in first, b = the number of elements in second, c = the number of elements in third. One of the following conditions will hold: a = b = c, a = b+1 = c+1, a = b = c+1. |
Examples
|
0) |
|
|
{"e","ello"}
|
{"el","hello"}
|
{"ell","ello"}
|
|
Returns: 2
|
The second player saw "hello" before making his second move, and then, the third player saw "ello" immediately after the second player made his second move. Therefore, the second player cheated on his second move because "hello" -> "ello" is not a legal move. |
|
|
1) |
|
|
{"de","coder","topcoder"}
|
{"der","pcoder","tipcoder"}
|
{"oder","opcoder","cheatcoder"}
|
|
Returns: 1
|
The first player cheated on his third turn. "topcoder" -> "tipcoder" is an illegal move. |
|
|
2) |
|
|
{"world","sworld"}
|
{"word"}
|
{"sword"}
|
|
Returns: 1
|
|
|
3) |
|
|
|
Returns: 3
|
In this case, the third player added more than one letter to the word. Note that the initial word may be empty. |
|
|
4) |
|
|
|
5) |
|
|
|
Returns: 1
|
The first player did not add a letter, so he is a cheater. |
|
|
6) |
|
|
{"e","wyve","vffwyve","puvffwyvef","bpuvffwyveftl", "tbpuvffwyveftlwz","ttbpuvffwyveftlwzcl", "ttbpuvffwyveftlwzcletq","uuttbpuvffwyveftlwzcletqh", "sjuuttbpuvffwyveftlwzcletqhd","vuysjuuttbpuvffwyveftlwzcletqhd", "vjvuysjuuttbpuvffwyveftlwzcletqhdn", "qsvjvuysjuuttbpuvffwyveftlwzcletqhdnn", "hmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj", "ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm", "ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudk", "jqophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"}
|
{"ve","fwyve","uvffwyve","puvffwyveft","bpuvffwyveftlw","ttbpuvffwyveftlwz", "ttbpuvffwyveftlwzcle","ttbpuvffwyveftlwzcletqh","uuttbpuvffwyveftlwzcletqhd", "ysjuuttbpuvffwyveftlwzcletqhd","jvuysjuuttbpuvffwyveftlwzcletqhd", "vjvuysjuuttbpuvffwyveftlwzcletqhdnn","qsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj", "hmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm", "ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmu", "ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"}
|
{"yve","ffwyve","uvffwyvef","puvffwyveftl","tbpuvffwyveftlw","ttbpuvffwyveftlwzc", "ttbpuvffwyveftlwzclet","uttbpuvffwyveftlwzcletqh","juuttbpuvffwyveftlwzcletqhd", "uysjuuttbpuvffwyveftlwzcletqhd","vjvuysjuuttbpuvffwyveftlwzcletqhd", "svjvuysjuuttbpuvffwyveftlwzcletqhdnn","mqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj", "phmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm", "ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmud", "qophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"}
|
|
Returns: -1
|
|
|
import java.util.*;
public class KidsWordGame {
int getCheater(String[] first, String[] second, String[] third) {
List<String> words = new ArrayList<String>();
try {
for (int i = 0; i < first.length; i++) {
words.add(first[i]);
words.add(second[i]);
words.add(third[i]);
}
} catch (Exception e) {}
for (int i = 0; i < words.size() - 1; i++)
if (!can_reach(words.get(i), words.get(i + 1)))
return i % 3 + 1;
return -1;
}
private boolean can_reach(String a, String b) {
if (b.length() - a.length() == 1 && b.indexOf(a) != -1)
return true;
return false;
}
}
分享到:
相关推荐
TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...
- **应用国家和地区**:主要用于中国市场,是中国自主研发的3G标准之一。 #### CDMA2000 (码分多址2000) - **技术改进**:相较于早期的CDMA技术,CDMA2000提高了数据传输速率,并增强了系统的容量。 - **应用地区**...
电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...
TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...
4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...
要告诉技术人员验证元素,请将“tchs”属性添加到元素。 <input type="text" tchs=""></input> 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...
此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...