using
System;
using
System.Collections.Generic;
public
class
Pair
{
public
int
first;
public
char
second;
public
Pair(
int
fi,
char
sec)
{
this
.first = fi;
this
.second = sec;
}
}
public
class
GFG
{
public
static
int
[,,] dp =
new
int
[1001,1001,26];
public
static
int
[,] ans =
new
int
[1001,26];
public
static
void
preprocess(String s)
{
var
n = s.Length;
for
(
int
i = 1; i <= n; i++)
{
GFG.dp[i,0,(
int
)(s[i - 1]) - (
int
)(
'a'
)] = 1;
}
for
(
int
i = 1; i <= n; i++)
{
for
(
int
j = 0; j <= i; j++)
{
for
(
int
k = 0; k < 26; k++)
{
if
(s[i - 1] == (
char
)(k + (
int
)(
'a'
)))
{
GFG.dp[i,j,k] = GFG.dp[i - 1,j,k] + 1;
}
else
if
(j != 0)
{
GFG.dp[i,j,k] = GFG.dp[i - 1,j - 1,k] + 1;
}
}
}
}
for
(
int
i = 1; i <= n; i++)
{
for
(
int
j = 0; j < 26; j++)
{
for
(
int
k = 1; k <= n; k++)
{
GFG.ans[i,j] = Math.Max(GFG.ans[i,j],GFG.dp[k,i,j]);
}
}
}
}
public
static
List<
int
> query(String s, Pair[] queries)
{
GFG.preprocess(s);
var
res =
new
List<
int
>();
for
(
int
i = 0; i < queries.Length; i++)
{
res.Add(GFG.ans[queries[i].first,(
int
)(queries[i].second) - (
int
)(
'a'
)]);
}
return
res;
}
public
static
void
Main(String[] args)
{
var
s =
"yamatonadeshiko"
;
Pair[] queries = {
new
Pair(1,
'a'
),
new
Pair(2,
'a'
),
new
Pair(3,
'a'
),
new
Pair(4,
'a'
),
new
Pair(5,
'a'
),
new
Pair(1,
'b'
),
new
Pair(2,
'b'
),
new
Pair(3,
'b'
),
new
Pair(4,
'b'
),
new
Pair(5,
'b'
)};
var
sol = GFG.query(s, queries);
foreach
(
int
x
in
sol)
{ Console.Write(
string
.Join(
", "
,x) +
" "
);
}
}
}