⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bruteforcestringsearcher.java

📁 BOOK:Beginning Algorithms Code Examples
💻 JAVA
字号:
package com.wrox.algorithms.ssearch;/** * A {@link StringSearcher} that uses a brute-force algorithm. * */public class BruteForceStringSearcher implements StringSearcher {    /** The pattern for which to search. */    private final CharSequence _pattern;    /**     * Constructor.     *     * @param pattern The pattern for which to search.     */    public BruteForceStringSearcher(CharSequence pattern) {        assert pattern != null : "pattern can't be null";        assert pattern.length() > 0 : "pattern can't be empty";        _pattern = pattern;    }    public StringMatch search(CharSequence text, int from) {        assert text != null : "text can't be null";        assert from >= 0 : "from can't be < 0";        int s = from;        while (s <= text.length() - _pattern.length()) {            int i = 0;            while (i < _pattern.length() && _pattern.charAt(i) == text.charAt(s + i)) {                ++i;            }            if (i == _pattern.length()) {                return new StringMatch(_pattern, text, s);            }            ++s;        }        return null;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -