此题有一个关键的地方在于确定好那个k之后,任意连续的k个字符组成的字符串都不相等才行。
也就是说在原字符串上随意截取长度为k的字串,不会截取到一摸一样的。
我们来想一想思路吧,
首先长度最大为100,我们以长度为1的字串为例,要查找100乘以100次也就是十的四次方,长度为100的话最多十的六次方。时间复杂度可以了,那么我们使用字符串hash来o1的查找,接着比对即可。
import java.awt.FontFormatException;
import java.awt.geom.AffineTransform;
import java.awt.image.BandCombineOp;
import java.awt.image.DataBufferInt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import javax.imageio.plugins.tiff.BaselineTIFFTagSet;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw1=new PrintWriter(System.out);
String[] aStrings=br1.readLine().split(" ");
int a=Integer.parseInt(aStrings[0]);
String bString=br1.readLine();
jiecheng[0]=1;
hash[0]=0;
int b;
for(b=1;b<a;b++) {
jiecheng[b]=(jiecheng[b-1]*bb)%cc;
}
for(b=1;b<=a;b++) {
hash[b]=(hash[b-1]*bb+bString.charAt(b-1)-'A'+1)%cc;
}
int answer=0;
int biaoji=0;
int c,d,e,f,g;
for(answer=1;answer<=a;answer++) {//字串长度的枚举
biaoji=0;//
for(b=1;b+answer-1<=a;b++) {
c=b+answer-1;
for(d=b+1;d+answer-1<=a;d++) {
e=d+answer-1;
long a1=((hash[c]-hash[b-1]*jiecheng[c-b+1])%cc+cc)%cc;
long a2=((hash[e]-hash[d-1]*jiecheng[e-d+1])%cc+cc)%cc;
if(a1==a2) {
biaoji=1;
break;
}
}
if(biaoji==1) {
break;
}
}
if(biaoji==0) {
System.out.println(answer);
return;
}
}
}
public static int aa=(int)(1e6+10);
public static int bb=131;
public static long cc=998244353;
public static long[] hash=new long[aa];
public static long[] jiecheng=new long[aa];
}