Codeforces 814 C. An impassioned circulation of affection (DP) __ Dynamic Planning

Source: Internet
Author: User
Tags lowercase
Description

Nadeko ' s birthday is approaching! As she decorated the room for the party, a long garland of dianthus-shaped paper pieces is placed on a prominent part of The wall. Brother Koyomi would like it!

Still unsatisfied with the Garland, Nadeko decided to polish it again. The Garland has n pieces numbered from-1 to N-to-right, and the i-th piece-has-a colour si, denoted by a Lowerca Se 中文版 Letter. Nadeko would repaint at most m of the pieces to give each of them a arbitrary new colour (still denoted by a lowercase Eng Lish letter). After this work, she finds out all subsegments of the garland containing pieces to only colour c-brother Koyomi ' s favour Ite one, and takes the length of the longest among them to be the koyomity of the Garland.

For instance, let ' s say the Garland be represented by ' Kooomo ', and Brother Koyomi ' s favourite ' o '. Among all subsegments containing pieces of ' o ' only, ' Ooo ' is the longest, with a length of 3. Thus the koyomity of this Garland equals 3.

But problem arises as Nadeko is unsure about Brother Koyomi ' s favourite colour, and has swaying ideas on the amount of WOR K to do. She has q plans on this, each of which can is expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are are to find out the maximum koyomity achievable after repainting the Garland to each plan.

Input

The "a" of input contains a positive integer n (1≤n≤1)-the length of the Garland.

The second line contains n lowercase 中文版 letters S1S2 ... sn as a string-the initial colours of paper pieces on the GAR Land.

The third line contains a positive integer q (1≤q≤200)-the number of plans Nadeko has.

The next Q lines describe one plan each:the i-th among them contains an MI (1≤mi≤n)-the maximum amount of P Ieces to repaint, followed by a spaces, then by a lowercase Chinese letter Ci-koyomi ' s possible favourite ' colour.

Output

Output Q lines:for Each work plan, output one line containing a integer-the largest koyomity achievable after Repainti Ng the Garland according to it.

examples Input

6
Koyomi
3
1 o
4 o
4 m

examples Output

3
6
5

the

Given a string, there is a Q-q query, each time the query tells you your favorite character and the number of times you want to replace the other characters, asking how long the string's favorite characters are.

train of Thought

DP[I][J] D p [i] [j] Dp[i][j] represents the maximum length that can be composed of the character "I I I", which can be substituted for J J J Times.

Then we can play out all the cases under the time complexity of O (n2) O (n 2) O (n^2), according to the query one by one output.

Of course this problem is also feasible, time complexity O (NXQ) o (NXQ) o (n \times q).

AC Code

#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring&
Gt
#include <algorithm> #define IO Ios::sync_with_stdio (false); \ cin.tie (0); \ cout.tie (0);
using namespace Std;
const int MAXN = 1E5+10;
const int mod = 1E9+7;

typedef long Long LL;
Char STR[MAXN];

int dp[26][maxn],n; 
            void Init () {for (int ch=0; ch<26; ch++) {for (int i=0; i<n; i++) {int now = 0;
                for (int j=i; j<n; J + +) {if (str[j]!= ' a ' +ch) now++;
            Dp[ch][now] = max (dp[ch][now],j-i+1);
    for (int i=1; i<=n; i++) dp[ch][i] = max (dp[ch][i],dp[ch][i-1));
    int main () {int q;
    cin>>n>>str>>q;
    Init ();
        while (q--) {int m;
        char c;
        cin>>m>>c;
    cout<<dp[c-' A '][m]<<endl;
return 0; }
Related Article

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.