You have just hacked into your friend's computer, and you want to delete one of his personal files. But wait, why not let him delete it! As you keep watching him, he has a delete command typed in on the terminal which contains a pattern. You want to rename the file to match this pattern so that it gets deleted once he issues the command. You must accomplish this by renaming the given file to a similar name. Other than lowercase letters, a pattern might contain '?' which represents exactly one character (so "a?b" matches "aab", "abb", "acb" etc). A move is defined as adding a character, deleting a character, or modifying a character from the filename. Given two Strings, file and pattern, return the minimal number of moves required to make the file match the pattern.
Problem Statement
Definition
Class:
WildCardMatch
Method:
minimalChanges
Parameters:
String, String
Returns:
int
Method signature:
int minimalChanges(String file, String pattern)
(be sure your method is public)
Constraints
-
file contains between 1 and 50 characters, inclusive.
-
pattern contains between 1 and 50 characters, inclusive.
-
Each character in file will be a lowercase letter ('a'-'z').
-
Each character in pattern will be a lowercase letter ('a'-'z') or '?'
Examples
0)
"abcd"
"bcd"
Returns: 1
Just remove the first character.
1)
"abcd"
"b??"
Returns: 1
Again the first character must be removed.
2)
"aaaabbb"
"aa????b"
Returns: 0
The file already matches the wildcard.
3)
"asdjkhajksdhajksdh"
"asdjkhasdjk?"
Returns: 6
4)
"niceone"
"ieo?e"
Returns: 2
5)
"abcaa"
"abcba"
Returns: 1
Modify just one character.
public class WildCardMatch {
public static int minimalChanges(String f, String p) {
int m = f.length(), n = p.length();
int[][] dp = new int[2][m+1];
for (int i = 0; i <= m; i++)
dp[0][i] = i;
for (int i = 0; i < n; i++) {
dp[1][0] = i + 1;
for (int j = 1; j <= m; j++) {
dp[1][j] = Math.min(dp[0][j], dp[1][j-1]) + 1;
int c = (p.charAt(i) == f.charAt(j-1)
|| p.charAt(i) == '?') ? 0 : 1;
dp[1][j] = Math.min(dp[1][j], dp[0][j-1] + c);
}
for (int j = 0; j <= m; j++)
dp[0][j] = dp[1][j];
}
return dp[1][m];
}
}
发表评论
-
TCHS-12-950
2010-03-01 12:58 790Problem Statement ... -
TCHS-12-550
2010-03-01 10:04 713Problem Statement ... -
TCHS-12-250
2010-02-28 09:53 725Problem Statement ... -
TCHS-11-1000
2010-02-22 15:22 679Problem Statement ... -
TCHS-11-500
2010-02-22 13:11 801Problem Statement ... -
TCHS-11-250
2010-02-22 11:38 727Problem Statement ... -
TCHS-10-1000
2010-02-21 16:35 697Problem Statement ... -
TCHS-10-500
2010-02-21 15:16 773Problem Statement ... -
TCHS-10-250
2010-02-21 14:26 773Problem Statement ... -
TCHS-9-1000
2010-02-20 18:45 708Problem Statement ... -
TCHS-9-500
2010-02-20 16:26 1335Problem Statement ... -
TCHS-9-250
2010-02-20 15:24 696Problem Statement ... -
TCHS-8-1000
2010-02-19 11:57 694Problem Statement ... -
TCHS-8-500
2010-02-19 10:56 768Problem Statement ... -
TCHS-8-250
2010-02-19 10:18 624Problem Statement ... -
TCHS-7-1000
2010-02-05 15:21 699Problem Statement ... -
TCHS-7-500
2010-02-05 13:45 699Problem Statement ... -
TCHS-7-250
2010-02-05 13:32 785Problem Statement ... -
TCHS-6-900
2010-02-04 12:06 680Problem Statement ... -
TCHS-6-250
2010-02-04 09:56 672Problem Statement ...
相关推荐
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`函数输出计算结果,包括基站数量、链路接...
BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...
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则专注于网页...