java indexof(字符串str)方法复杂性
可能重复:java indexof(字符串str)方法复杂性,java,algorithm,Java,Algorithm,可能重复: java indexof(stringstr)方法的复杂性是什么。我的意思是有一些字符串匹配算法,比如KMP,它在线性时间内运行。我正在实现一个需要在一个非常大的字符串中搜索大的子字符串的系统,所以我可以使用java indexof(string str)方法,还是应该实现KMP。java的indexof的复杂性是O(m*n)其中n和m分别是搜索字符串和模式的长度 要提高复杂性,您可以使用算法,例如,智能地跳过比较字符串中与模式不匹配的逻辑部分 javaindexOf函数复杂性为O
java indexof(stringstr)方法的复杂性是什么。我的意思是有一些字符串匹配算法,比如KMP,它在线性时间内运行。我正在实现一个需要在一个非常大的字符串中搜索大的子字符串的系统,所以我可以使用java indexof(string str)方法,还是应该实现KMP。java的indexof的复杂性是
O(m*n)
其中n
和m
分别是搜索字符串和模式的长度
要提高复杂性,您可以使用算法,例如,智能地跳过比较字符串中与模式不匹配的逻辑部分 java
indexOf
函数复杂性为O(n*m)
其中n为文本长度,m为模式长度这是原始代码的索引
/**
* Returns the index within this string of the first occurrence of the
* specified substring. The integer returned is the smallest value
* <i>k</i> such that:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* is <code>true</code>.
*
* @param str any string.
* @return if the string argument occurs as a substring within this
* object, then the index of the first character of the first
* such substring is returned; if it does not occur as a
* substring, <code>-1</code> is returned.
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* Returns the index within this string of the first occurrence of the
* specified substring, starting at the specified index. The integer
* returned is the smallest value <tt>k</tt> for which:
* <blockquote><pre>
* k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* @param str the substring for which to search.
* @param fromIndex the index from which to start the search.
* @return the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, offset, count,
str.value, str.offset, str.count, fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
*如果不存在这样的k值,则返回-1。
*
*@param str要搜索的子字符串。
*@param fromIndex开始搜索的索引。
*@返回此字符串中第一次出现的
*指定的子字符串,从指定的索引开始。
*/
公共int-indexOf(字符串str,int-fromIndex){
返回indexOf(值、偏移量、计数、,
str.value、str.offset、str.count、fromIndex);
}
/**
*由String和StringBuffer共享以执行搜索的代码。这个
*源是正在搜索的字符数组,目标是
*正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param sourceOffset源字符串的偏移量。
*@param sourceCount源字符串的计数。
*@param目标是要搜索的字符。
*@param targetOffset目标字符串的偏移量。
*@param targetCount目标字符串的计数。
*@param fromIndex开始搜索的索引。
*/
静态int indexOf(char[]source,int sourceOffset,int sourceCount,
char[]目标,int targetOffset,int targetCount,
int fromIndex){
if(fromIndex>=sourceCount){
返回(targetCount==0?sourceCount:-1);
}
如果(从索引<0){
fromIndex=0;
}
如果(targetCount==0){
从索引返回;
}
char first=目标[targetOffset];
int max=sourceOffset+(sourceCount-targetCount);
对于(int i=sourceOffset+fromIndex;i),您应该使用方便的方法,除非您有硬数据表明它不会削减它。在提出带有模糊定义的假设性问题时没有用(“真的大”有多长?15000字符?5 GB?)在互联网上。-1,你的答案是错误的/误导性的。有两种长度需要担心——你正在搜索的字符串和你正在搜索的字符串——算法是O(mn)。@LouisWasserman,对,谢谢你指出。
O(m*n)
是最坏的情况,不典型。典型的是O(n)
。当你说O(m*n)
您完全忽略了匹配模式对当前位置的短路,该短路会在模式不匹配时立即停止。这很少会超过模式的第三个字符。indexOf()
甚至在进入模式匹配逻辑之前都有特定的优化来寻找第一个字符。摊销后,它可能类似于O(1.1*n)
,所以O(n)
。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main{
int failure[];
int i,j;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
String pat="",str="";
public Main(){
try{
int patLength=Integer.parseInt(in.readLine());
pat=in.readLine();
str=in.readLine();
fillFailure(pat,patLength);
match(str,pat,str.length(),patLength);
out.println();
failure=null;}catch(Exception e){}
out.flush();
}
public void fillFailure(String pat,int patLen){
failure=new int[patLen];
failure[0]=-1;
for(i=1;i<patLen;i++){
j=failure[i-1];
while(j>=0&&pat.charAt(j+1)!=pat.charAt(i))
j=failure[j];
if(pat.charAt(j+1)==pat.charAt(i))
failure[i]=j+1;
else
failure[i]=-1;
}
}
public void match(String str,String pat,int strLen,int patLen){
i=0;
j=0;
while(i<strLen){
if(str.charAt(i)==pat.charAt(j)){
i++;
j++;
if(j==patLen){
out.println(i-j);
j=failure[j-1]+1;
}
} else if (j==0){
i++;
}else{
j=failure[j-1]+1;
}
}
}
public static void main(String[] args) {
new Main();
}
}