【问题标题】:String Searching - CodeEval - Trouble identifying cases that break code字符串搜索 - CodeEval - 无法识别破坏代码的案例
【发布时间】:2015-10-13 23:50:48
【问题描述】:

*这不是一个活跃的竞赛/竞赛问题。

我在以下问题上取得了重大进展(我的得分为 92.5/100),但在过去的几个小时里,我一直在疯狂地摸索我没有考虑哪些情况,因此不见了。

这是问题陈述:

给你两个字符串。确定第二个字符串是否是第一个字符串的子字符串(不要使用任何 substr 类型库函数)。第二个字符串可能包含一个星号(*),应将其视为正则表达式,即匹配零个或多个字符;星号可以用 \ 字符转义,在这种情况下,它应该被解释为常规的 '*' 字符。总结一下:第一个字符串可以包含字母、数字和'*',第二个字符串可以包含字母、数字、\和'*'。

下面是我的动态编程方法:

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();
}

以下是我的代码当前正确生成的一些输入和相关答案:

So*eaE,C*E
废话**废话*,bl*h\*
你好,嗯
这很好,是
CodeEval,C*Eval
老了,年轻了


真的
真的
真的
真的
假的

CodeEval 不允许您查看测试用例。所以我很感激有人帮助我找出破坏我的代码的测试用例,这样我就可以修复我的代码逻辑。

【问题讨论】:

    标签: string dynamic-programming


    【解决方案1】:

    第一个字符串,第二个字符串,实际结果,预期结果

    *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
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2017-12-31
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多