problems / 198

DNA Palindrome Markers

easy strings palindromes dp prefix-sums 1000 ms · 256 MB

DNA Palindrome Markers

In molecular biology, palindromic sequences in DNA are significant because they often represent binding sites for enzymes or markers for genetic analysis. A palindromic DNA sequence reads the same forwards and backwards.

Alice is analyzing a DNA strand (represented as a string of characters) and needs to quickly answer queries about whether certain regions contain palindromic markers.

Given a DNA strand and q queries, each query asks: is the substring from position i to position j (inclusive, 1-indexed) a palindrome?

Input

The first line contains the DNA strand s (1 ≤ |s| ≤ 1000).

The second line contains the number of queries q (1 ≤ q ≤ 100000).

Each of the next q lines contains two integers i and j (1 ≤ i ≤ j ≤ |s|) representing a query range.

Output

For each query, print YES if the substring s[i..j] is a palindrome, or NO otherwise.

Notes

A palindrome reads the same forwards and backwards. For example, "ACA" is a palindrome, but "ACG" is not.

Positions are 1-indexed, so the first character of the strand is at position 1.

Example 1

Input

RACECAR
5
1 7
1 3
2 4
3 5
1 1

Output

YES
NO
NO
YES
YES

Example 2

Input

ABBA
6
1 4
1 2
2 3
1 1
4 4
2 4

Output

YES
NO
YES
YES
YES
NO

Hints · reveal one at a time if you're stuck

Hint 1

There are up to 10^5 queries but the strand has at most 1000 characters. What could you precompute about every substring so that each query is a simple lookup?

Hint 2

A substring is a palindrome when its two end characters match and the part between them is a palindrome. Fill the table in order of increasing length so the inner part is always ready first.

report an issue Prefer your own editor? sooks prep 198 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.