给定一个字符串str和Q查询。每个查询由两个数字L和R 组成。任务是打印子字符串 [L…R]是否是回文。
例子:
Input: str = “abacccde”, Q[][] = {{0, 2}, {1, 2}, {2, 4}, {3, 5}}
Output:
Yes
No
No
Yes
Input: str = “abaaab”, Q[][] = {{0, 1}, {1, 5}}
Output:
No
Yes
简单的方法:一种简单的方法是检查每个子字符串是否为回文。在最坏的情况下,每个查询最多需要O(Q) 。
高效的方法: 一种有效的方法是使用动态规划来解决上述问题。我们可以最初创建DP表,该表存储substring[i….j]是否为回文。我们维护一个以自下而上的方式填充的布尔值dp[n][n] 。如果子串是回文, dp[i][j] 的值为真,否则为假。要计算 dp[i][j],我们首先检查dp[i+1][j-1]的值,如果该值为真并且s[i]与s[j]相同,那么我们使dp[i][j]正确。否则, dp[i][j] 的值为假。
现在,对于每个查询,检查dp[l][r]是否为真。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define N 100
// Pre-processing function
void pre_process(bool dp[N][N], string s)
{
// Get the size of the string
int n = s.size();
// Initially mark every
// position as false
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
dp[i][j] = false;
}
// For the length
for (int j = 1; j <= n; j++) {
// Iterate for every index with
// length j
for (int i = 0; i <= n - j; i++) {
// If the length is less than 2
if (j <= 2) {
// If characters are equal
if (s[i] == s[i + j - 1])
dp[i][i + j - 1] = true;
}
// Check for equal
else if (s[i] == s[i + j - 1])
dp[i][i + j - 1] = dp[i + 1][i + j - 2];
}
}
}
// Function to answer every query in O(1)
void answerQuery(int l, int r, bool dp[N][N])
{
if (dp[l][r])
cout << "Yes\n";
else
cout << "No\n";
}
// Driver code
int main()
{
string s = "abaaab";
bool dp[N][N];
pre_process(dp, s);
int queries[][2] = { { 0, 1 }, { 1, 5 } };
int q = sizeof(queries) / sizeof(queries[0]);
for (int i = 0; i < q; i++)
answerQuery(queries[i][0], queries[i][1], dp);
return 0;
}
Java
// Java implementation of the approach
class GFG {
static int N = 100;
// Pre-processing function
static void pre_process(boolean dp[][], char[] s)
{
// Get the size of the string
int n = s.length;
// Initially mark every
// position as false
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = false;
}
}
// For the length
for (int j = 1; j <= n; j++) {
// Iterate for every index with
// length j
for (int i = 0; i <= n - j; i++) {
// If the length is less than 2
if (j <= 2) {
// If characters are equal
if (s[i] == s[i + j - 1]) {
dp[i][i + j - 1] = true;
}
}
// Check for equal
else if (s[i] == s[i + j - 1]) {
dp[i][i + j - 1] = dp[i + 1][i + j - 2];
}
}
}
}
// Function to answer every query in O(1)
static void answerQuery(int l, int r, boolean dp[][])
{
if (dp[l][r]) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
// Driver code
public static void main(String[] args)
{
String s = "abaaab";
boolean[][] dp = new boolean[N][N];
pre_process(dp, s.toCharArray());
int queries[][] = { { 0, 1 }, { 1, 5 } };
int q = queries.length;
for (int i = 0; i < q; i++) {
answerQuery(queries[i][0], queries[i][1], dp);
}
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
N = 100
# Pre-processing function
def pre_process(dp, s):
# Get the size of the string
n = len(s)
# Initially mark every
# position as false
for i in range(n):
for j in range(n):
dp[i][j] = False
# For the length
for j in range(1, n + 1):
# Iterate for every index with
# length j
for i in range(n - j + 1):
# If the length is less than 2
if (j <= 2):
# If characters are equal
if (s[i] == s[i + j - 1]):
dp[i][i + j - 1] = True
# Check for equal
elif (s[i] == s[i + j - 1]):
dp[i][i + j - 1] = dp[i + 1][i + j - 2]
# Function to answer every query in O(1)
def answerQuery(l, r, dp):
if (dp[l][r]):
print("Yes")
else:
print("No")
# Driver code
s = "abaaab"
dp = [[0 for i in range(N)]
for i in range(N)]
pre_process(dp, s)
queries = [[0, 1], [1, 5]]
q = len(queries)
for i in range(q):
answerQuery(queries[i][0],
queries[i][1], dp)
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG {
static int N = 100;
// Pre-processing function
static void pre_process(bool[, ] dp, char[] s)
{
// Get the size of the string
int n = s.Length;
// Initially mark every
// position as false
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i, j] = false;
}
}
// For the length
for (int j = 1; j <= n; j++) {
// Iterate for every index with
// length j
for (int i = 0; i <= n - j; i++) {
// If the length is less than 2
if (j <= 2) {
// If characters are equal
if (s[i] == s[i + j - 1]) {
dp[i, i + j - 1] = true;
}
}
// Check for equal
else if (s[i] == s[i + j - 1]) {
dp[i, i + j - 1] = dp[i + 1, i + j - 2];
}
}
}
}
// Function to answer every query in O(1)
static void answerQuery(int l, int r, bool[, ] dp)
{
if (dp[l, r]) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
// Driver code
public static void Main()
{
string s = "abaaab";
bool[, ] dp = new bool[N, N];
pre_process(dp, s.ToCharArray());
int[, ] queries = { { 0, 1 }, { 1, 5 } };
int q = queries.Length;
for (int i = 0; i < q; i++) {
answerQuery(queries[i, 0], queries[i, 1], dp);
}
}
}
// This code is contributed by Ankit.
PHP
Javascript
输出:
No
Yes
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。