51nod 1732 51nod Marriage Institute

Source: Internet
Author: User

51nod In addition to doing OJ, but also carried out a lot of sideline. Marriage introduction is one of them.
For a customer, we can use a string to describe the characteristics of the customer.
Suppose we now have two customers A and B.
The character string for a is: ABCDEFGB's character string is: abcxyz
Then A and B match degrees F (A, B) is the length of the longest public prefix of a and B, that is Len (' abc ') = 3
Because of the recent 51nod budget, the jacket master designed a compression algorithm to conserve memory.
The trait strings for all users are stored in a string s of length n. (n <= 1000) The user's trait is represented by an integer p, which indicates that the user's trait string is s[p...n-1].
Now given the string s, and q query <ai, bi> (AI, Bi is a legitimate user trait integer). Please output the Q query corresponding to the customer matching degree.



Input
Now given the string length n, with the string s. Next is the integer q, which represents the next Q-Query. The following Q line has two integer ai, bi. Represents the matching degree of the query trait for users of AI and bi. 1 <= n <= 10001 <= q <= 10^6 input data all valid.
Output
Each row outputs a user-matching integer.
Input example
12loveornolove53 70 09 13 19 5
Output example
012300
Wizmann(topic Provider)Idea: Directly reverse the string, and then Dp[i][j] represents the first and the longest public suffix of J, the last query directly output DP[N-U][N-V] good, O (n^2) preprocessing, O (1) query.
#include <iostream> #include <algorithm> #include <cstdio> #include <queue> #include <map > #include <vector> #include <cstring> #include <cmath>using namespace std;typedef long Long ll;    const int INF =0x3f3f3f3f;const double pi = ACOs ( -1.0); const int N = 1e3 + 10;int Dp[n][n];char s[n],x[n];int main () {    int n, m;    scanf ("%d", &n);    scanf ("%s", s);    for (int i = 0; i<n; i++) {x[n-i-1] = s[i];    } X[n] = ' + ';    memset (DP, 0, sizeof (DP));                for (int i = 1, i<=n; i++) for (int j = 1; j<=n; j + +) {if (x[i-1] = = X[j-1]) {            DP[I][J] = dp[i-1][j-1] + 1;        } else Dp[i][j] = 0;        } int u, v;        scanf ("%d", &m);            while (m--) {scanf ("%d%d", &u, &v);        printf ("%d\n", Dp[n-u][n-v]); } return 0;}


51nod 1732 51nod Marriage Institute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.