算法
Java是真的麻烦,没有unsigned类型,所以只能对2^63取模,相当于减去MIN_VALUE。
按照之前的经验,因为JAVA的IO很慢,所以只能存到sb里面,最后统一输出。
Java 代码
import java.util.Scanner;
class Main {
private static final int base = 131;
private static final long INF = Long.MIN_VALUE;
private static final String Y = "Yes";
private static final String N = "No";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int m = in.nextInt();
int[][] q = new int[m][4];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 4; ++j) {
q[i][j] = in.nextInt();
}
}
in.close();
solve(s, q);
}
private static long get(long[] hash, long[] pow, int l, int r) {
long right = hash[r], left = hash[l - 1];
long ans = right - left * pow[r - l + 1];
if (ans < 0) ans -= INF;
return ans;
}
private static long[] pow(int n) {
long[] pow = new long[n];
pow[0] = 1;
for (int i = 1; i < n; ++i) {
pow[i] = pow[i - 1] * base;
if (pow[i] < 0) pow[i] -= INF;
}
return pow;
}
private static long[] hash(String s) {
long[] hash = new long[s.length() + 1];
hash[0] = 0;
for (int i = 0; i < s.length(); ++i) {
hash[i + 1] = base * hash[i] + s.charAt(i) - 'a' + 1;
if (hash[i + 1] < 0) {
hash[i + 1] -= INF;
}
}
return hash;
}
private static void solve(String s, int[][] query) {
StringBuilder sb = new StringBuilder();
long[] pow = pow(s.length() + 1);
long[] hash = hash(s);
for (int[] q : query) {
if (get(hash, pow, q[0], q[1]) == get(hash, pow, q[2], q[3])) {
sb.append(Y);
} else {
sb.append(N);
}
sb.append("\n");
}
System.out.print(sb);
}
}
同感,溢出直接变负太麻烦了