2015-10-13 27 views
0

*這不是一個活躍的競賽/競賽問題。字符串搜索 - CodeEval - 查找破解代碼的案例

我在以下問題上取得了重大突破(我的得分爲92.5/100),但在過去的幾個小時裏我一直在瘋狂地撓頭,以至於我不考慮什麼情況,因此失蹤。

以下是問題聲明:

給您兩個字符串。確定第二個字符串是否是第一個字符串的子字符串(不要使用任何substr類型庫函數)。第二個字符串可能包含星號(*),應該將其作爲正則表達式處理,即匹配零個或多個字符;星號可以通過\ char轉義,在這種情況下,它應該被解釋爲一個常規的'*'字符。總結一下:第一個字符串可以包含字母,數字和'*',第二個字符串可以包含字母,數字,和'*'。

這下面是我的動態規劃方法:

private static String REGEX0 = "\\*"; 
private static String REGEX1 = "\\\\\\*"; 
private static String REPLACE = "#"; 

public static void main(String[] args) throws IOException { 
    File file = new File(args[0]); 
    BufferedReader buffer = new BufferedReader(new FileReader(file)); 
    String line; 
    while ((line = buffer.readLine()) != null) { 
     line = line.trim(); 
     if (line.isEmpty()) continue; 
     String[] sar = line.split(","); 
     Pattern pattern0 = Pattern.compile(REGEX0); 
     Pattern pattern1 = Pattern.compile(REGEX1); 
     Matcher matcher0 = pattern0.matcher(sar[0]); 
     Matcher matcher1 = pattern1.matcher(sar[1]); 
     sar[0] = matcher0.replaceAll(REPLACE); 
     sar[1] = matcher1.replaceAll(REPLACE); 
     System.out.println(find(sar[0], sar[1])); 
    } 
} 

private static boolean find(String r, String c) { 
    int[][] match = new int[r.length() + 1][c.length() + 1]; 
    for (int i = 1; i <= r.length(); i++) { 
     for (int j = 1; j <= c.length(); j++) { 
      if (r.charAt(i - 1) == c.charAt(j - 1)) { 
       match[i][j] = match[i - 1][j - 1] + 1; 
      } else if (c.charAt(j - 1) == '*') { 
       if (match[i - 1][j - 1] == 0) { 
        match[i][j] = Math.max(match[i - 1][j], match[i][j - 1]); 
       } else { 
        match[i][j] = match[i - 1][j - 1] + 1; 
       } 
      } 
      if (match[i][j] == c.length()) return true; 
     } 
    } 
    return c.length() > r.length() && match[r.length()][c.length()] == r.length(); 
} 

這裏有一些投入和相關的答案,這我的代碼目前正常生產:

所以* EAE,C * E
嗒嗒**嗒嗒*,BL * H \ *
你好,埃爾
這是好事,是
CodeEval,C *評估和演示
老,幼


真正
真正
真正

CodeEval不會讓你看到測試用例。所以我會很感激有人幫我弄清楚破壞我的代碼的測試用例,所以我可以修復我的代碼邏輯。

回答

1

1弦,2弦,實際結果,預期結果

*So*eaE, *JH*, false, false 
*So*eaE, **, false, true(?) 
blblah*, blb*h*, true, true 
blah*, blb*h*, false, false 
blah*, blb*h, false, false 
CodeEval, C*Eval, true, true 
CodeEval, C*Ev*al, false, true 
CodeE*val, C*Ev*al, false, false 
Old, *Young*, false,  false 
Old, *ung*, false,  false 
Old, *l*, false,   true (?) 
Old, O*l*, true,   true 
Old, *d, false,   true 
Old, O*, true,   true 
Old, O*d, true,   true