对应AcWing841 import java.util.*; class Main { // 定义一个进制:131 或者13131,这里采用了long,是因为long会自动处理溢出。本来是需要采用膜2^64来处理冲突的 static final int N = 100010, P = 131; static long[] h = new long[N]; static long[] p = new long[N]; // 此处相当于移位 // ABCD 四位数,当要减去高位的数字AB时,需要把AB先进行移位,位数为A的位置-C的位置。 private static long get(int l, int r) { return h[r] - h[l - 1] * p[r - l + 1]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String str = sc.next(); p[0] = 1; // 相当于一个初始化操作,把P的n位的数存起来,并且把1-n的前缀给求出来 for (int i = 1; i <= n; i ++) { p[i] = p[i - 1] * P; h[i] = h[i - 1] * P + str.charAt(i - 1); } for (int i = 0; i < k; i ++) { int l1 = sc.nextInt(), r1 = sc.nextInt(), l2 = sc.nextInt(), r2 = sc.nextInt(); System.out.println(get(l1, r1) == get(l2, r2) ? "Yes" : "No" ); } } }