AcWing 841. 字符串哈希-java
原题链接
简单
作者:
Susu
,
2020-01-29 16:24:34
,
所有人可见
,
阅读 818
import java.io.*;
public class Main{
static int N = 100010;
static int P = 131;
static char[] c = new char[N];
static long[] p = new long[N];
static long[] h = new long[N];
static long get(int l,int r){
return h[r]-h[l-1]*p[r-l+1];
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int n = Integer.valueOf(s[0]);
int m = Integer.valueOf(s[1]);
String str = br.readLine();
p[0] = 1;
for(int i=1;i<=n;i++){
c[i] = str.charAt(i-1);
p[i] = p[i-1]*P;
h[i] = h[i-1]*P + c[i];
}
while(m-->0){
String[] strs = br.readLine().split(" ");
int l1 = Integer.valueOf(strs[0]);int r1 = Integer.valueOf(strs[1]);
int l2 = Integer.valueOf(strs[2]);int r2 = Integer.valueOf(strs[3]);
if(get(l1,r1)==get(l2,r2)) System.out.println("Yes");
else System.out.println("No");
}
}
}
只是long 不是无符号 很遗憾
我也在想这个问题,java到底怎么写.大佬现在找到解决方法了吗?
Java里long类型不会溢出吗= =