1
我們給出了兩個Pattern.A模式只包含大寫/小寫英文字母和星號(*)。
星號可以匹配zero and four
之間的字母。
例如,GoneGirl和GoneTomorrow匹配模式Gone **,但帶有標題TheGoneGirl和GoneWithTheWind的書不能。
Question LinkCode Jam字符串匹配
我們必須找出是否有字符串匹配匹配兩種模式。
我的方法: 動態編程維護一個二維數組。
M[0][0]=true;
for(int i=1;i<=S.length();i++){
for(int j=1;j<=A.length();j++){
if(!M[i-1][j-1]) continue;
if(S.charAt(i-1)=='*'){
for(int k=0;k<4;k++) M[i][j+k]=true;
M[i][j-1]=true;
}
if(A.charAt(j-1)=='*'){
for(int k=0;k<4;k++) M[i+k][j]=true;
M[i-1][j]=true;
}
if(S.charAt(i-1)==A.charAt(j-1)) M[i][j]=true;
}
有人可以幫我在我的算法中出了什麼問題嗎?
第一步應該是找到失敗的測試用例。 –
相關:https://stackoverflow.com/questions/34009784/checking-collision-in-filename-search-patterns-with-wildcards/ – m69