QOJ.ac

QOJ

時間限制: 2 s 記憶體限制: 128 MB 總分: 100

#6361. String

统计

For her birthday, Jia Yuan's friend bought her a gift from an online store. The gift is inside a magical box. On the outside of the box, there is a string $s$ of length $n$ and $m$ questions. Jia Yuan must answer these $m$ questions correctly to open the box, get the gift, get a promotion and a raise, become a CEO, marry a rich and handsome person, and reach the pinnacle of her life.

Each question consists of four parameters $a, b, c, d$. You are asked to find the maximum length of the longest common prefix between any substring of $s[a..b]$ and the substring $s[c..d]$.

Jia Yuan is not good at solving such problems, so she is asking for your help. How can you help her?

Input

The first line contains two positive integers $n$ and $m$, representing the length of the string and the number of queries, respectively.

The next line contains a string of length $n$.

The next $m$ lines each contain four integers $a, b, c, d$, representing the query for the maximum length of the longest common prefix between any substring of $s[a..b]$ and the substring $s[c..d]$.

Output

For each query, output the answer.

Constraints

  • For 10% of the data, $1 \le n, m \le 300$.
  • For 40% of the data, $1 \le n, m \le 3,000$, and the string contains only 'a' and 'b'.
  • For 100% of the data, $1 \le n, m \le 100,000$, the string contains only lowercase English letters, $a \le b$, $c \le d$, and $1 \le a, b, c, d \le n$.

Examples

Input 1

5 5
aaaaa
1 1 1 5
1 5 1 1
2 3 2 3
2 4 2 3
2 3 2 4

Output 1

1
1
2
2
2

Input 2

5 5
ababa
1 1 2 2
2 3 1 1
1 2 2 3
1 3 3 5
1 3 2 4

Output 2

0
1
1
3
2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.